Skip to main content

harn_vm/stdlib/
process.rs

1use crate::value::VmDictExt;
2use std::cell::RefCell;
3use std::collections::BTreeMap;
4use std::io::Write as _;
5use std::path::PathBuf;
6use std::process::Stdio;
7use std::sync::mpsc;
8use std::time::{Duration, Instant};
9
10use crate::orchestration::RunExecutionRecord;
11use crate::stdlib::macros::{harn_builtin, VmBuiltinDef};
12use crate::value::{VmError, VmValue};
13use crate::vm::Vm;
14
15const HARN_REPLAY_ENV: &str = "HARN_REPLAY";
16
17thread_local! {
18    pub(crate) static VM_SOURCE_DIR: RefCell<Option<PathBuf>> = const { RefCell::new(None) };
19    static VM_EXECUTION_CONTEXT: RefCell<Option<RunExecutionRecord>> = const { RefCell::new(None) };
20}
21
22/// Set the source directory for the current thread (called by VM on file execution).
23pub(crate) fn set_thread_source_dir(dir: &std::path::Path) {
24    VM_SOURCE_DIR.with(|sd| *sd.borrow_mut() = Some(normalize_context_path(dir)));
25}
26
27pub(crate) fn normalize_context_path(path: &std::path::Path) -> PathBuf {
28    if path.is_absolute() {
29        return path.to_path_buf();
30    }
31    std::env::current_dir()
32        .map(|cwd| cwd.join(path))
33        .unwrap_or_else(|_| path.to_path_buf())
34}
35
36pub fn set_thread_execution_context(context: Option<RunExecutionRecord>) {
37    VM_EXECUTION_CONTEXT.with(|current| *current.borrow_mut() = context);
38}
39
40pub(crate) fn current_execution_context() -> Option<RunExecutionRecord> {
41    VM_EXECUTION_CONTEXT.with(|current| current.borrow().clone())
42}
43
44/// Reset thread-local process state (for test isolation).
45pub(crate) fn reset_process_state() {
46    VM_SOURCE_DIR.with(|sd| *sd.borrow_mut() = None);
47    VM_EXECUTION_CONTEXT.with(|current| *current.borrow_mut() = None);
48}
49
50pub fn execution_root_path() -> PathBuf {
51    current_execution_context()
52        .and_then(|context| context.cwd.map(PathBuf::from))
53        .or_else(|| std::env::current_dir().ok())
54        .unwrap_or_else(|| PathBuf::from("."))
55}
56
57pub fn source_root_path() -> PathBuf {
58    VM_SOURCE_DIR
59        .with(|sd| sd.borrow().clone())
60        .or_else(|| {
61            current_execution_context().and_then(|context| context.source_dir.map(PathBuf::from))
62        })
63        .or_else(|| current_execution_context().and_then(|context| context.cwd.map(PathBuf::from)))
64        .or_else(|| std::env::current_dir().ok())
65        .unwrap_or_else(|| PathBuf::from("."))
66}
67
68pub fn asset_root_path() -> PathBuf {
69    source_root_path()
70}
71
72fn env_override(name: &str) -> Option<String> {
73    (name == HARN_REPLAY_ENV && crate::triggers::dispatcher::current_dispatch_is_replay())
74        .then(|| "1".to_string())
75}
76
77pub(crate) fn read_env_value(name: &str) -> Option<String> {
78    env_override(name)
79        .or_else(|| current_execution_context().and_then(|context| context.env.get(name).cloned()))
80        .or_else(|| std::env::var(name).ok())
81}
82
83pub fn runtime_root_base() -> PathBuf {
84    find_project_root(&execution_root_path())
85        .or_else(|| find_project_root(&source_root_path()))
86        .unwrap_or_else(source_root_path)
87}
88
89/// Lexically collapse `..` components in `path`. Returns `None` if a
90/// `..` would pop a non-Normal component (i.e. the path tries to walk
91/// above its root anchor). This is a pure-string canonicalization that
92/// does NOT hit the filesystem — symlinks are not followed.
93fn lexically_collapse(path: &std::path::Path) -> Option<PathBuf> {
94    use std::path::Component;
95    let mut out: Vec<Component> = Vec::new();
96    for component in path.components() {
97        match component {
98            Component::CurDir => {}
99            Component::ParentDir => {
100                let popped = out.pop();
101                if !matches!(popped, Some(Component::Normal(_))) {
102                    return None;
103                }
104            }
105            other => out.push(other),
106        }
107    }
108    Some(out.iter().collect())
109}
110
111pub fn resolve_source_relative_path(path: &str) -> PathBuf {
112    let candidate = PathBuf::from(path);
113    if candidate.is_absolute() {
114        return candidate;
115    }
116    let root = execution_root_path();
117    let joined = root.join(&candidate);
118    // Defense-in-depth path-traversal check (paired with the deferred
119    // F3 sandbox-by-default fix): refuse to resolve a path that
120    // escapes the project root via `..` components. We anchor against
121    // `runtime_root_base()` (the project root), which is broader than
122    // `execution_root_path()` and lets benign sibling-dir walks like
123    // `read_file("../fixtures/payload.json")` from `tests/` succeed.
124    if path_escapes_project_root(&joined) {
125        return root.join("__harn_rejected_parent_dir_traversal__");
126    }
127    joined
128}
129
130pub fn resolve_source_asset_path(path: &str) -> PathBuf {
131    let candidate = PathBuf::from(path);
132    if candidate.is_absolute() {
133        return candidate;
134    }
135    let root = asset_root_path();
136    let joined = root.join(&candidate);
137    if path_escapes_project_root(&joined) {
138        return root.join("__harn_rejected_parent_dir_traversal__");
139    }
140    joined
141}
142
143/// Returns `true` when `joined` (which may contain raw `..`
144/// components) cannot be lexically collapsed without popping past its
145/// root component — i.e. the relative input had more `..` than the
146/// joined depth allows, escaping the filesystem root.
147///
148/// This is intentionally a narrow check: it doesn't try to enforce
149/// that the path stays inside a logical "project root", because the
150/// project root isn't always reliably resolvable (and benign uses
151/// like `../fixtures/x.json` from a `tests/` subdir are legitimate).
152/// The sandbox layer remains the authoritative defense for arbitrary
153/// `..` traversal; this guard plugs the most egregious escapes
154/// (`../../../../etc/passwd`) for the no-sandbox-by-default
155/// `harn run` path.
156fn path_escapes_project_root(joined: &std::path::Path) -> bool {
157    lexically_collapse(joined).is_none()
158}
159
160pub(crate) fn register_process_builtins(vm: &mut Vm) {
161    for def in PROCESS_BUILTINS {
162        vm.register_builtin_def(def);
163    }
164}
165
166#[harn_builtin(sig = "env(name: string) -> string?", category = "process")]
167fn env_impl(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
168    let name = args.first().map(|a| a.display()).unwrap_or_default();
169    if let Some(value) = read_env_value(&name) {
170        return Ok(VmValue::String(std::sync::Arc::from(value)));
171    }
172    Ok(VmValue::Nil)
173}
174
175#[harn_builtin(
176    sig = "env_or(name: string, default: any) -> any",
177    category = "process"
178)]
179fn env_or_impl(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
180    let name = args.first().map(|a| a.display()).unwrap_or_default();
181    let default = args.get(1).cloned().unwrap_or(VmValue::Nil);
182    if let Some(value) = read_env_value(&name) {
183        return Ok(VmValue::String(std::sync::Arc::from(value)));
184    }
185    Ok(default)
186}
187
188#[harn_builtin(sig = "exit(code?: int) -> never", category = "process")]
189fn exit_impl(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
190    let code = args.first().and_then(|a| a.as_int()).unwrap_or(0);
191    std::process::exit(code as i32);
192}
193
194#[harn_builtin(sig = "exec(...command: string) -> dict", category = "process")]
195fn exec_impl(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
196    if args.is_empty() {
197        return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
198            "exec: command is required",
199        ))));
200    }
201    let cmd = args[0].display();
202    let cmd_args: Vec<String> = args[1..].iter().map(|a| a.display()).collect();
203    let output = exec_command(None, &cmd, &cmd_args)?;
204    Ok(vm_output_to_value(output))
205}
206
207#[harn_builtin(sig = "shell(command: string) -> dict", category = "process")]
208fn shell_impl(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
209    let cmd = args.first().map(|a| a.display()).unwrap_or_default();
210    if cmd.is_empty() {
211        return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
212            "shell: command string is required",
213        ))));
214    }
215    let invocation = crate::shells::default_shell_invocation(&cmd)
216        .map_err(|error| VmError::Runtime(format!("shell: {error}")))?;
217    let output = exec_shell_args(None, &invocation.program, &invocation.args)?;
218    Ok(vm_output_to_value(output))
219}
220
221#[harn_builtin(
222    sig = "exec_at(dir: string, ...command: string) -> dict",
223    category = "process"
224)]
225fn exec_at_impl(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
226    if args.len() < 2 {
227        return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
228            "exec_at: directory and command are required",
229        ))));
230    }
231    let dir = args[0].display();
232    let cmd = args[1].display();
233    let cmd_args: Vec<String> = args[2..].iter().map(|a| a.display()).collect();
234    let output = exec_command(Some(dir.as_str()), &cmd, &cmd_args)?;
235    Ok(vm_output_to_value(output))
236}
237
238#[harn_builtin(
239    sig = "shell_at(dir: string, command: string) -> dict",
240    category = "process"
241)]
242fn shell_at_impl(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
243    if args.len() < 2 {
244        return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
245            "shell_at: directory and command string are required",
246        ))));
247    }
248    let dir = args[0].display();
249    let cmd = args[1].display();
250    if cmd.is_empty() {
251        return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
252            "shell_at: command string is required",
253        ))));
254    }
255    let invocation = crate::shells::default_shell_invocation(&cmd)
256        .map_err(|error| VmError::Runtime(format!("shell_at: {error}")))?;
257    let output = exec_shell_args(Some(dir.as_str()), &invocation.program, &invocation.args)?;
258    Ok(vm_output_to_value(output))
259}
260
261#[harn_builtin(sig = "username(...args: any) -> string", category = "process")]
262fn username_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
263    let user = std::env::var("USER")
264        .or_else(|_| std::env::var("USERNAME"))
265        .unwrap_or_default();
266    Ok(VmValue::String(std::sync::Arc::from(user)))
267}
268
269#[harn_builtin(sig = "hostname() -> string", category = "process")]
270fn hostname_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
271    let name = std::env::var("HOSTNAME")
272        .or_else(|_| std::env::var("COMPUTERNAME"))
273        .or_else(|_| {
274            std::process::Command::new("hostname")
275                .output()
276                .ok()
277                .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
278                .ok_or(std::env::VarError::NotPresent)
279        })
280        .unwrap_or_default();
281    Ok(VmValue::String(std::sync::Arc::from(name)))
282}
283
284#[harn_builtin(sig = "platform(...args: any) -> string", category = "process")]
285fn platform_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
286    let os = if cfg!(target_os = "macos") {
287        "darwin"
288    } else if cfg!(target_os = "linux") {
289        "linux"
290    } else if cfg!(target_os = "windows") {
291        "windows"
292    } else {
293        std::env::consts::OS
294    };
295    Ok(VmValue::String(std::sync::Arc::from(os)))
296}
297
298#[harn_builtin(sig = "arch() -> string", category = "process")]
299fn arch_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
300    Ok(VmValue::String(std::sync::Arc::from(
301        std::env::consts::ARCH,
302    )))
303}
304
305#[harn_builtin(sig = "home_dir() -> string", category = "process")]
306fn home_dir_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
307    let home = crate::user_dirs::home_dir()
308        .map(|home| home.to_string_lossy().into_owned())
309        .unwrap_or_default();
310    Ok(VmValue::String(std::sync::Arc::from(home)))
311}
312
313#[harn_builtin(sig = "pid(...args: any) -> int", category = "process")]
314fn pid_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
315    Ok(VmValue::Int(std::process::id() as i64))
316}
317
318#[harn_builtin(sig = "date_iso() -> string", category = "process")]
319fn date_iso_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
320    // `date_iso` reads the OS wall clock directly (it predates the
321    // unified `clock_mock`). Routing through `leak_audit::wall_now`
322    // keeps the production behavior unchanged but surfaces the call
323    // in `testbench_clock_leaks()` whenever a script invokes it
324    // under a paused testbench session, so fidelity hazards are
325    // visible instead of silently corrupting tapes.
326    let now = crate::clock_mock::leak_audit::wall_now("stdlib/date_iso");
327    let dt: chrono::DateTime<chrono::Utc> = now.into();
328    Ok(VmValue::String(std::sync::Arc::from(
329        dt.to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
330    )))
331}
332
333#[harn_builtin(sig = "cwd() -> string", category = "process")]
334fn cwd_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
335    let dir = current_execution_context()
336        .and_then(|context| context.cwd)
337        .or_else(|| {
338            std::env::current_dir()
339                .ok()
340                .map(|p| p.to_string_lossy().into_owned())
341        })
342        .unwrap_or_default();
343    Ok(VmValue::String(std::sync::Arc::from(dir)))
344}
345
346#[harn_builtin(sig = "execution_root() -> string", category = "process")]
347fn execution_root_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
348    Ok(VmValue::String(std::sync::Arc::from(
349        execution_root_path().to_string_lossy().into_owned(),
350    )))
351}
352
353#[harn_builtin(sig = "asset_root() -> string", category = "process")]
354fn asset_root_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
355    Ok(VmValue::String(std::sync::Arc::from(
356        asset_root_path().to_string_lossy().into_owned(),
357    )))
358}
359
360#[harn_builtin(sig = "runtime_paths() -> dict", category = "process")]
361fn runtime_paths_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
362    let runtime_base = runtime_root_base();
363    let mut paths = BTreeMap::new();
364    paths.put_str("execution_root", execution_root_path().to_string_lossy());
365    paths.put_str("asset_root", asset_root_path().to_string_lossy());
366    paths.put_str(
367        "state_root",
368        crate::runtime_paths::state_root(&runtime_base).to_string_lossy(),
369    );
370    paths.put_str(
371        "run_root",
372        crate::runtime_paths::run_root(&runtime_base).to_string_lossy(),
373    );
374    paths.put_str(
375        "worktree_root",
376        crate::runtime_paths::worktree_root(&runtime_base).to_string_lossy(),
377    );
378    Ok(VmValue::dict(paths))
379}
380
381#[harn_builtin(sig = "spawn_captured(opts: dict) -> dict", category = "process")]
382fn spawn_captured_impl(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
383    spawn_captured_value(args)
384}
385
386// `term_width()` / `term_height()` return the current terminal
387// dimensions in columns and rows. Reads `COLUMNS` / `LINES` env vars
388// first (so test harnesses can pin a value), falls back to the
389// platform `ioctl` size, and finally defaults to 80x24 when neither
390// is available (e.g. when stdout is not a TTY). These are the
391// free-builtin aliases for `harness.term.width()` /
392// `harness.term.height()`. `std/tui` already exposes
393// `__tui_terminal_width` for its renderer; these aliases keep
394// ported subcommands working without importing the tui module.
395#[harn_builtin(sig = "term_width() -> int", category = "process")]
396fn term_width_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
397    Ok(VmValue::Int(crate::term::width() as i64))
398}
399
400#[harn_builtin(sig = "term_height() -> int", category = "process")]
401fn term_height_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
402    Ok(VmValue::Int(crate::term::height() as i64))
403}
404
405const PROCESS_BUILTINS: &[&VmBuiltinDef] = &[
406    &ENV_IMPL_DEF,
407    &ENV_OR_IMPL_DEF,
408    &EXIT_IMPL_DEF,
409    &EXEC_IMPL_DEF,
410    &EXEC_OPTS_IMPL_DEF,
411    &SHELL_IMPL_DEF,
412    &EXEC_AT_IMPL_DEF,
413    &EXEC_AT_OPTS_IMPL_DEF,
414    &SHELL_AT_IMPL_DEF,
415    &USERNAME_IMPL_DEF,
416    &HOSTNAME_IMPL_DEF,
417    &PLATFORM_IMPL_DEF,
418    &ARCH_IMPL_DEF,
419    &HOME_DIR_IMPL_DEF,
420    &PID_IMPL_DEF,
421    &DATE_ISO_IMPL_DEF,
422    &CWD_IMPL_DEF,
423    &EXECUTION_ROOT_IMPL_DEF,
424    &ASSET_ROOT_IMPL_DEF,
425    &RUNTIME_PATHS_IMPL_DEF,
426    &SPAWN_CAPTURED_IMPL_DEF,
427    &TERM_WIDTH_IMPL_DEF,
428    &TERM_HEIGHT_IMPL_DEF,
429];
430
431/// Run an external command synchronously and return captured output.
432///
433/// Shared by the legacy free builtin and `harness.process.spawn_captured` so
434/// subprocess capture has one implementation and one result shape.
435pub(crate) fn spawn_captured_value(args: &[VmValue]) -> Result<VmValue, VmError> {
436    let opts = match args.first() {
437        Some(VmValue::Dict(opts)) => opts.clone(),
438        _ => {
439            return Err(VmError::Runtime(
440                "spawn_captured: options dict is required".to_string(),
441            ));
442        }
443    };
444    let cmd = match opts.get("cmd").map(|v| v.display()).unwrap_or_default() {
445        s if s.is_empty() => {
446            return Err(VmError::Runtime(
447                "spawn_captured: opts.cmd is required".to_string(),
448            ));
449        }
450        s => s,
451    };
452    let cmd_args: Vec<String> = match opts.get("args") {
453        Some(VmValue::List(items)) => items.iter().map(|v| v.display()).collect(),
454        None | Some(VmValue::Nil) => Vec::new(),
455        Some(other) => {
456            return Err(VmError::Runtime(format!(
457                "spawn_captured: opts.args must be a list of strings, got {}",
458                other.type_name()
459            )));
460        }
461    };
462    let cwd = opts
463        .get("cwd")
464        .map(|v| v.display())
465        .filter(|s| !s.is_empty());
466    let env_overrides: Vec<(String, String)> = match opts.get("env") {
467        Some(VmValue::Dict(env)) => env.iter().map(|(k, v)| (k.clone(), v.display())).collect(),
468        None | Some(VmValue::Nil) => Vec::new(),
469        Some(other) => {
470            return Err(VmError::Runtime(format!(
471                "spawn_captured: opts.env must be a dict, got {}",
472                other.type_name()
473            )));
474        }
475    };
476    let stdin_bytes: Option<Vec<u8>> = match opts.get("stdin") {
477        Some(VmValue::Bytes(bytes)) => Some(bytes.as_slice().to_vec()),
478        Some(VmValue::String(s)) => Some(s.as_bytes().to_vec()),
479        None | Some(VmValue::Nil) => None,
480        Some(other) => {
481            return Err(VmError::Runtime(format!(
482                "spawn_captured: opts.stdin must be string or bytes, got {}",
483                other.type_name()
484            )));
485        }
486    };
487    let timeout = opts
488        .get("timeout_ms")
489        .and_then(|v| v.as_int())
490        .filter(|n| *n > 0)
491        .map(|n| Duration::from_millis(n as u64));
492
493    let spawn = CapturedSpawn {
494        label: "spawn_captured",
495        cmd: &cmd,
496        args: &cmd_args,
497        cwd: cwd.as_deref(),
498        env: &env_overrides,
499        // `spawn_captured` has always layered `env` over the inherited
500        // parent environment, so keep that merge behavior.
501        env_clear: false,
502        stdin: stdin_bytes,
503        timeout,
504    };
505    let CapturedRun {
506        output,
507        timed_out,
508        duration_ms,
509    } = run_captured_spawn(spawn)?;
510
511    let exit_code = if timed_out {
512        -1
513    } else {
514        output.status.code().unwrap_or(-1) as i64
515    };
516    let success = if timed_out {
517        false
518    } else {
519        output.status.success()
520    };
521    let mut result = BTreeMap::new();
522    result.insert("exit_code".to_string(), VmValue::Int(exit_code));
523    result.put_str("stdout", String::from_utf8_lossy(&output.stdout).as_ref());
524    result.put_str("stderr", String::from_utf8_lossy(&output.stderr).as_ref());
525    result.insert("duration_ms".to_string(), VmValue::Int(duration_ms));
526    result.insert("success".to_string(), VmValue::Bool(success));
527    result.insert("timed_out".to_string(), VmValue::Bool(timed_out));
528    Ok(VmValue::dict(result))
529}
530
531/// Parameters for [`run_captured_spawn`]: a single synchronous subprocess
532/// spawn that captures stdout/stderr, optionally feeds stdin, optionally
533/// enforces a wall-clock timeout, and either merges (`env_clear == false`)
534/// or replaces (`env_clear == true`) the parent environment with `env`.
535struct CapturedSpawn<'a> {
536    label: &'static str,
537    cmd: &'a str,
538    args: &'a [String],
539    cwd: Option<&'a str>,
540    env: &'a [(String, String)],
541    env_clear: bool,
542    stdin: Option<Vec<u8>>,
543    timeout: Option<Duration>,
544}
545
546/// Result of [`run_captured_spawn`].
547struct CapturedRun {
548    output: std::process::Output,
549    timed_out: bool,
550    duration_ms: i64,
551}
552
553/// Shared synchronous spawn-and-capture core used by `spawn_captured` and the
554/// `exec_opts`/`exec_at_opts` convenience builtins. Honors cwd, an env
555/// overlay (merge or replace via `env_clear`), optional stdin, and an optional
556/// wall-clock timeout (after which the child is killed and `timed_out` is set).
557fn run_captured_spawn(spec: CapturedSpawn<'_>) -> Result<CapturedRun, VmError> {
558    let label = spec.label;
559    let mut command = std::process::Command::new(spec.cmd);
560    command.args(spec.args);
561    if let Some(cwd) = spec.cwd {
562        command.current_dir(cwd);
563    }
564    if spec.env_clear {
565        command.env_clear();
566    }
567    for (key, value) in spec.env {
568        command.env(key, value);
569    }
570    command.stdout(Stdio::piped()).stderr(Stdio::piped());
571    if spec.stdin.is_some() {
572        command.stdin(Stdio::piped());
573    } else {
574        command.stdin(Stdio::null());
575    }
576
577    let started = Instant::now();
578    let cmd = spec.cmd;
579    let mut child = command.spawn().map_err(|error| {
580        VmError::Thrown(VmValue::String(std::sync::Arc::from(format!(
581            "{label}: failed to spawn '{cmd}': {error}"
582        ))))
583    })?;
584
585    if let (Some(payload), Some(mut stdin)) = (spec.stdin, child.stdin.take()) {
586        // Children may close stdin early while still producing useful output.
587        let _ = stdin.write_all(&payload);
588    }
589
590    let (output, timed_out) = match spec.timeout {
591        None => match child.wait_with_output() {
592            Ok(output) => (output, false),
593            Err(error) => {
594                return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
595                    format!("{label}: wait failed: {error}"),
596                ))));
597            }
598        },
599        Some(limit) => {
600            let deadline = started + limit;
601            let mut timed_out = false;
602            loop {
603                match child.try_wait() {
604                    Ok(Some(_)) => break,
605                    Ok(None) => {
606                        if Instant::now() >= deadline {
607                            let _ = child.kill();
608                            let _ = child.wait();
609                            timed_out = true;
610                            break;
611                        }
612                        std::thread::sleep(Duration::from_millis(10));
613                    }
614                    Err(error) => {
615                        return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
616                            format!("{label}: poll failed: {error}"),
617                        ))));
618                    }
619                }
620            }
621            if timed_out {
622                let stdout_handle = child.stdout.take();
623                let stderr_handle = child.stderr.take();
624                let (tx_out, rx_out) = mpsc::channel::<Vec<u8>>();
625                let (tx_err, rx_err) = mpsc::channel::<Vec<u8>>();
626                if let Some(mut s) = stdout_handle {
627                    std::thread::spawn(move || {
628                        use std::io::Read as _;
629                        let mut buf = Vec::new();
630                        let _ = s.read_to_end(&mut buf);
631                        let _ = tx_out.send(buf);
632                    });
633                }
634                if let Some(mut s) = stderr_handle {
635                    std::thread::spawn(move || {
636                        use std::io::Read as _;
637                        let mut buf = Vec::new();
638                        let _ = s.read_to_end(&mut buf);
639                        let _ = tx_err.send(buf);
640                    });
641                }
642                let stdout = rx_out
643                    .recv_timeout(Duration::from_millis(100))
644                    .unwrap_or_default();
645                let stderr = rx_err
646                    .recv_timeout(Duration::from_millis(100))
647                    .unwrap_or_default();
648                (
649                    std::process::Output {
650                        status: std::process::ExitStatus::default(),
651                        stdout,
652                        stderr,
653                    },
654                    true,
655                )
656            } else {
657                match child.wait_with_output() {
658                    Ok(output) => (output, false),
659                    Err(error) => {
660                        return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
661                            format!("{label}: wait failed: {error}"),
662                        ))));
663                    }
664                }
665            }
666        }
667    };
668
669    Ok(CapturedRun {
670        output,
671        timed_out,
672        duration_ms: started.elapsed().as_millis() as i64,
673    })
674}
675
676/// Parsed `exec_opts` / `exec_at_opts` options, ready to populate a
677/// [`CapturedSpawn`].
678#[derive(Default)]
679struct ExecOptions {
680    env: Vec<(String, String)>,
681    env_clear: bool,
682    cwd: Option<String>,
683    timeout: Option<Duration>,
684}
685
686/// Extract `exec_opts` / `exec_at_opts` options into an [`ExecOptions`].
687///
688/// `env_mode` mirrors the `process.exec` host op (and the env-clear footgun
689/// fix): the default is `"merge"` (overlay `env` keys on the inherited parent
690/// environment, keeping PATH/HOME/etc.); `"replace"` clears the parent
691/// environment first so only the provided keys remain.
692fn exec_options(label: &str, options: Option<&VmValue>) -> Result<ExecOptions, VmError> {
693    let opts = match options {
694        None | Some(VmValue::Nil) => return Ok(ExecOptions::default()),
695        Some(VmValue::Dict(opts)) => opts.clone(),
696        Some(other) => {
697            return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
698                format!("{label}: options must be a dict, got {}", other.type_name()),
699            ))));
700        }
701    };
702    let env: Vec<(String, String)> = match opts.get("env") {
703        Some(VmValue::Dict(env)) => env.iter().map(|(k, v)| (k.clone(), v.display())).collect(),
704        None | Some(VmValue::Nil) => Vec::new(),
705        Some(other) => {
706            return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
707                format!(
708                    "{label}: options.env must be a dict, got {}",
709                    other.type_name()
710                ),
711            ))));
712        }
713    };
714    let env_clear = match opts.get("env_mode").map(|v| v.display()).as_deref() {
715        None | Some("merge") => false,
716        Some("replace") => true,
717        Some(other) => {
718            return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
719                format!(
720                    "{label}: options.env_mode must be \"merge\" or \"replace\", got {other:?}"
721                ),
722            ))));
723        }
724    };
725    let cwd = opts
726        .get("cwd")
727        .map(|v| v.display())
728        .filter(|s| !s.is_empty());
729    // Accept both `timeout` and `timeout_ms` (millis), matching the
730    // `process.exec` host op's tolerance.
731    let timeout = opts
732        .get("timeout")
733        .or_else(|| opts.get("timeout_ms"))
734        .and_then(|v| v.as_int())
735        .filter(|n| *n > 0)
736        .map(|n| Duration::from_millis(n as u64));
737    Ok(ExecOptions {
738        env,
739        env_clear,
740        cwd,
741        timeout,
742    })
743}
744
745/// Build the `exec`-shaped result dict (`stdout`/`stderr`/`status`/`success`)
746/// and additionally surface `timed_out` so options-form callers can detect a
747/// timeout kill without inspecting the exit status.
748fn captured_run_to_value(run: &CapturedRun) -> VmValue {
749    let status = if run.timed_out {
750        -1
751    } else {
752        run.output.status.code().unwrap_or(-1) as i64
753    };
754    let success = !run.timed_out && run.output.status.success();
755    let mut result = BTreeMap::new();
756    result.put_str(
757        "stdout",
758        String::from_utf8_lossy(&run.output.stdout).as_ref(),
759    );
760    result.put_str(
761        "stderr",
762        String::from_utf8_lossy(&run.output.stderr).as_ref(),
763    );
764    result.insert("status".to_string(), VmValue::Int(status));
765    result.insert("success".to_string(), VmValue::Bool(success));
766    result.insert("timed_out".to_string(), VmValue::Bool(run.timed_out));
767    result.insert("duration_ms".to_string(), VmValue::Int(run.duration_ms));
768    VmValue::dict(result)
769}
770
771#[harn_builtin(
772    sig = "exec_opts(command: list, options: dict?) -> dict",
773    category = "process"
774)]
775fn exec_opts_impl(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
776    let command = exec_opts_command("exec_opts", args.first())?;
777    let opts = exec_options("exec_opts", args.get(1))?;
778    let run = run_captured_spawn(CapturedSpawn {
779        label: "exec_opts",
780        cmd: &command[0],
781        args: &command[1..],
782        cwd: opts.cwd.as_deref(),
783        env: &opts.env,
784        env_clear: opts.env_clear,
785        stdin: None,
786        timeout: opts.timeout,
787    })?;
788    Ok(captured_run_to_value(&run))
789}
790
791#[harn_builtin(
792    sig = "exec_at_opts(dir: string, command: list, options: dict?) -> dict",
793    category = "process"
794)]
795fn exec_at_opts_impl(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
796    let dir = match args.first() {
797        Some(value) if !value.display().is_empty() => value.display(),
798        _ => {
799            return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
800                "exec_at_opts: directory is required",
801            ))));
802        }
803    };
804    let command = exec_opts_command("exec_at_opts", args.get(1))?;
805    let opts = exec_options("exec_at_opts", args.get(2))?;
806    // The positional `dir` argument is the working directory; an explicit
807    // `options.cwd` (rare) overrides it so callers retain full control.
808    let resolved_cwd = opts.cwd.unwrap_or(dir);
809    let run = run_captured_spawn(CapturedSpawn {
810        label: "exec_at_opts",
811        cmd: &command[0],
812        args: &command[1..],
813        cwd: Some(resolved_cwd.as_str()),
814        env: &opts.env,
815        env_clear: opts.env_clear,
816        stdin: None,
817        timeout: opts.timeout,
818    })?;
819    Ok(captured_run_to_value(&run))
820}
821
822/// Validate the `command` argument shared by `exec_opts`/`exec_at_opts`: a
823/// non-empty list whose first element is a non-empty program name.
824fn exec_opts_command(label: &str, value: Option<&VmValue>) -> Result<Vec<String>, VmError> {
825    let items = match value {
826        Some(VmValue::List(items)) => items,
827        _ => {
828            return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
829                format!("{label}: command must be a non-empty list of strings"),
830            ))));
831        }
832    };
833    let command: Vec<String> = items.iter().map(|v| v.display()).collect();
834    if command.is_empty() || command[0].is_empty() {
835        return Err(VmError::Thrown(VmValue::String(std::sync::Arc::from(
836            format!("{label}: command must be a non-empty list of strings"),
837        ))));
838    }
839    Ok(command)
840}
841
842/// Find the project root by walking up from a base directory looking for harn.toml.
843pub fn find_project_root(base: &std::path::Path) -> Option<std::path::PathBuf> {
844    let mut dir = base.to_path_buf();
845    loop {
846        if dir.join("harn.toml").exists() {
847            return Some(dir);
848        }
849        if !dir.pop() {
850            return None;
851        }
852    }
853}
854
855/// Register builtins that depend on source directory context.
856pub(crate) fn register_path_builtins(vm: &mut Vm) {
857    for def in PATH_BUILTINS {
858        vm.register_builtin_def(def);
859    }
860}
861
862#[harn_builtin(sig = "source_dir(...args: any) -> string", category = "process")]
863fn source_dir_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
864    let dir = VM_SOURCE_DIR.with(|sd| sd.borrow().clone());
865    match dir {
866        Some(d) => Ok(VmValue::String(std::sync::Arc::from(
867            d.to_string_lossy().into_owned(),
868        ))),
869        None => {
870            let cwd = std::env::current_dir()
871                .map(|p| p.to_string_lossy().into_owned())
872                .unwrap_or_default();
873            Ok(VmValue::String(std::sync::Arc::from(cwd)))
874        }
875    }
876}
877
878#[harn_builtin(sig = "project_root() -> string?", category = "process")]
879fn project_root_impl(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
880    let base = current_execution_context()
881        .and_then(|context| context.cwd.map(PathBuf::from))
882        .or_else(|| VM_SOURCE_DIR.with(|sd| sd.borrow().clone()))
883        .or_else(|| std::env::current_dir().ok())
884        .unwrap_or_else(|| PathBuf::from("."));
885    match find_project_root(&base) {
886        Some(root) => Ok(VmValue::String(std::sync::Arc::from(
887            root.to_string_lossy().into_owned(),
888        ))),
889        None => Ok(VmValue::Nil),
890    }
891}
892
893const PATH_BUILTINS: &[&VmBuiltinDef] = &[&SOURCE_DIR_IMPL_DEF, &PROJECT_ROOT_IMPL_DEF];
894
895fn vm_output_to_value(output: std::process::Output) -> VmValue {
896    let mut result = BTreeMap::new();
897    result.put_str("stdout", String::from_utf8_lossy(&output.stdout).as_ref());
898    result.put_str("stderr", String::from_utf8_lossy(&output.stderr).as_ref());
899    result.insert(
900        "status".to_string(),
901        VmValue::Int(output.status.code().unwrap_or(-1) as i64),
902    );
903    result.insert(
904        "success".to_string(),
905        VmValue::Bool(output.status.success()),
906    );
907    VmValue::dict(result)
908}
909
910fn exec_command(
911    dir: Option<&str>,
912    cmd: &str,
913    args: &[String],
914) -> Result<std::process::Output, VmError> {
915    let config = process_command_config(dir)?;
916    crate::stdlib::sandbox::command_output(cmd, args, &config)
917        .map_err(|error| prefix_process_error(error, "exec"))
918}
919
920#[cfg(test)]
921fn exec_shell(
922    dir: Option<&str>,
923    shell: &str,
924    flag: &str,
925    script: &str,
926) -> Result<std::process::Output, VmError> {
927    let args = vec![flag.to_string(), script.to_string()];
928    exec_shell_args(dir, shell, &args)
929}
930
931fn exec_shell_args(
932    dir: Option<&str>,
933    shell: &str,
934    args: &[String],
935) -> Result<std::process::Output, VmError> {
936    let config = process_command_config(dir)?;
937    crate::stdlib::sandbox::command_output(shell, args, &config)
938        .map_err(|error| prefix_process_error(error, "shell"))
939}
940
941fn process_command_config(
942    dir: Option<&str>,
943) -> Result<crate::stdlib::sandbox::ProcessCommandConfig, VmError> {
944    let mut config = crate::stdlib::sandbox::ProcessCommandConfig {
945        stdin_null: true,
946        ..Default::default()
947    };
948    if let Some(dir) = dir {
949        let resolved = resolve_command_dir(dir);
950        crate::stdlib::sandbox::enforce_process_cwd(&resolved)?;
951        config.cwd = Some(resolved);
952    } else if let Some(context) = current_execution_context() {
953        if let Some(cwd) = context.cwd.filter(|cwd| !cwd.is_empty()) {
954            crate::stdlib::sandbox::enforce_process_cwd(std::path::Path::new(&cwd))?;
955            config.cwd = Some(std::path::PathBuf::from(cwd));
956        }
957        if !context.env.is_empty() {
958            config.env.extend(context.env);
959        }
960    }
961    if let Some(value) = env_override(HARN_REPLAY_ENV) {
962        config.env.push((HARN_REPLAY_ENV.to_string(), value));
963    }
964    Ok(config)
965}
966
967fn prefix_process_error(error: VmError, prefix: &str) -> VmError {
968    match error {
969        VmError::Thrown(VmValue::String(message)) => VmError::Thrown(VmValue::String(
970            std::sync::Arc::from(format!("{prefix} failed: {message}")),
971        )),
972        other => other,
973    }
974}
975
976fn resolve_command_dir(dir: &str) -> PathBuf {
977    let candidate = PathBuf::from(dir);
978    if candidate.is_absolute() {
979        return candidate;
980    }
981    if let Some(cwd) = current_execution_context().and_then(|context| context.cwd) {
982        return PathBuf::from(cwd).join(candidate);
983    }
984    if let Some(source_dir) = VM_SOURCE_DIR.with(|sd| sd.borrow().clone()) {
985        return source_dir.join(candidate);
986    }
987    candidate
988}
989
990#[cfg(test)]
991mod tests {
992    use super::*;
993
994    struct RuntimePathsEnvGuard {
995        state: Option<String>,
996        run: Option<String>,
997        worktree: Option<String>,
998    }
999
1000    impl RuntimePathsEnvGuard {
1001        fn capture() -> Self {
1002            Self {
1003                state: std::env::var(crate::runtime_paths::HARN_STATE_DIR_ENV).ok(),
1004                run: std::env::var(crate::runtime_paths::HARN_RUN_DIR_ENV).ok(),
1005                worktree: std::env::var(crate::runtime_paths::HARN_WORKTREE_DIR_ENV).ok(),
1006            }
1007        }
1008    }
1009
1010    impl Drop for RuntimePathsEnvGuard {
1011        fn drop(&mut self) {
1012            match self.state.as_deref() {
1013                Some(value) => std::env::set_var(crate::runtime_paths::HARN_STATE_DIR_ENV, value),
1014                None => std::env::remove_var(crate::runtime_paths::HARN_STATE_DIR_ENV),
1015            }
1016            match self.run.as_deref() {
1017                Some(value) => std::env::set_var(crate::runtime_paths::HARN_RUN_DIR_ENV, value),
1018                None => std::env::remove_var(crate::runtime_paths::HARN_RUN_DIR_ENV),
1019            }
1020            match self.worktree.as_deref() {
1021                Some(value) => {
1022                    std::env::set_var(crate::runtime_paths::HARN_WORKTREE_DIR_ENV, value);
1023                }
1024                None => std::env::remove_var(crate::runtime_paths::HARN_WORKTREE_DIR_ENV),
1025            }
1026        }
1027    }
1028
1029    #[test]
1030    fn lexically_collapse_resolves_sibling_walk() {
1031        let path = PathBuf::from("/tmp/project/tests/../fixtures/x.json");
1032        let collapsed = lexically_collapse(&path).expect("sibling walk");
1033        assert_eq!(collapsed, PathBuf::from("/tmp/project/fixtures/x.json"));
1034    }
1035
1036    #[test]
1037    fn lexically_collapse_blocks_escape_past_root() {
1038        // `/app/../etc/passwd` would lexically resolve to `/etc/passwd`,
1039        // but the pop hits a RootDir which is not Normal — refuse.
1040        let path = PathBuf::from("/app/../../etc/passwd");
1041        assert!(lexically_collapse(&path).is_none());
1042    }
1043
1044    #[test]
1045    fn lexically_collapse_strips_curdir() {
1046        let path = PathBuf::from("/app/./logs/today.txt");
1047        let collapsed = lexically_collapse(&path).expect("curdir is benign");
1048        assert_eq!(collapsed, PathBuf::from("/app/logs/today.txt"));
1049    }
1050
1051    #[test]
1052    fn resolve_source_relative_path_blocks_obvious_escape() {
1053        let dir =
1054            std::env::temp_dir().join(format!("harn-process-escape-{}", uuid::Uuid::now_v7()));
1055        std::fs::create_dir_all(&dir).unwrap();
1056        set_thread_source_dir(&dir);
1057        set_thread_execution_context(Some(crate::orchestration::RunExecutionRecord {
1058            cwd: Some(dir.to_string_lossy().into_owned()),
1059            source_dir: Some(dir.to_string_lossy().into_owned()),
1060            env: BTreeMap::new(),
1061            adapter: None,
1062            repo_path: None,
1063            worktree_path: None,
1064            branch: None,
1065            base_ref: None,
1066            cleanup: None,
1067        }));
1068        // A long string of `..` should escape the temp-root and trip
1069        // the rejection sentinel, so the file read fails NotFound
1070        // instead of escaping to a different filesystem location.
1071        let resolved = resolve_source_relative_path("../../../../../../../../etc/passwd");
1072        assert!(
1073            resolved
1074                .to_string_lossy()
1075                .contains("__harn_rejected_parent_dir_traversal__"),
1076            "expected rejection sentinel, got {resolved:?}"
1077        );
1078        reset_process_state();
1079        let _ = std::fs::remove_dir_all(&dir);
1080    }
1081
1082    #[test]
1083    fn resolve_source_relative_path_ignores_thread_source_dir_without_execution_context() {
1084        let dir = std::env::temp_dir().join(format!("harn-process-{}", uuid::Uuid::now_v7()));
1085        std::fs::create_dir_all(&dir).unwrap();
1086        let current_dir = std::env::current_dir().unwrap();
1087        set_thread_source_dir(&dir);
1088        let resolved = resolve_source_relative_path("templates/prompt.txt");
1089        assert_eq!(resolved, current_dir.join("templates/prompt.txt"));
1090        reset_process_state();
1091        let _ = std::fs::remove_dir_all(&dir);
1092    }
1093
1094    #[test]
1095    fn resolve_source_relative_path_prefers_execution_cwd_over_source_dir() {
1096        let cwd = std::env::temp_dir().join(format!("harn-process-cwd-{}", uuid::Uuid::now_v7()));
1097        let source_dir =
1098            std::env::temp_dir().join(format!("harn-process-source-{}", uuid::Uuid::now_v7()));
1099        std::fs::create_dir_all(&cwd).unwrap();
1100        std::fs::create_dir_all(&source_dir).unwrap();
1101        set_thread_source_dir(&source_dir);
1102        set_thread_execution_context(Some(crate::orchestration::RunExecutionRecord {
1103            cwd: Some(cwd.to_string_lossy().into_owned()),
1104            source_dir: Some(source_dir.to_string_lossy().into_owned()),
1105            env: BTreeMap::new(),
1106            adapter: None,
1107            repo_path: None,
1108            worktree_path: None,
1109            branch: None,
1110            base_ref: None,
1111            cleanup: None,
1112        }));
1113        let resolved = resolve_source_relative_path("templates/prompt.txt");
1114        assert_eq!(resolved, cwd.join("templates/prompt.txt"));
1115        reset_process_state();
1116        let _ = std::fs::remove_dir_all(&cwd);
1117        let _ = std::fs::remove_dir_all(&source_dir);
1118    }
1119
1120    #[test]
1121    fn resolve_source_asset_path_prefers_execution_source_dir_over_cwd() {
1122        let cwd = std::env::temp_dir().join(format!("harn-asset-cwd-{}", uuid::Uuid::now_v7()));
1123        let source_dir =
1124            std::env::temp_dir().join(format!("harn-asset-source-{}", uuid::Uuid::now_v7()));
1125        std::fs::create_dir_all(&cwd).unwrap();
1126        std::fs::create_dir_all(&source_dir).unwrap();
1127        set_thread_source_dir(&source_dir);
1128        set_thread_execution_context(Some(crate::orchestration::RunExecutionRecord {
1129            cwd: Some(cwd.to_string_lossy().into_owned()),
1130            source_dir: Some(source_dir.to_string_lossy().into_owned()),
1131            env: BTreeMap::new(),
1132            adapter: None,
1133            repo_path: None,
1134            worktree_path: None,
1135            branch: None,
1136            base_ref: None,
1137            cleanup: None,
1138        }));
1139        let resolved = resolve_source_asset_path("templates/prompt.txt");
1140        assert_eq!(resolved, source_dir.join("templates/prompt.txt"));
1141        reset_process_state();
1142        let _ = std::fs::remove_dir_all(&cwd);
1143        let _ = std::fs::remove_dir_all(&source_dir);
1144    }
1145
1146    #[test]
1147    fn set_thread_source_dir_absolutizes_relative_paths() {
1148        reset_process_state();
1149        let current_dir = std::env::current_dir().unwrap();
1150        set_thread_source_dir(std::path::Path::new("scripts"));
1151        assert_eq!(source_root_path(), current_dir.join("scripts"));
1152        reset_process_state();
1153    }
1154
1155    #[test]
1156    fn exec_context_sets_default_cwd_and_env() {
1157        let dir = std::env::temp_dir().join(format!("harn-process-ctx-{}", uuid::Uuid::now_v7()));
1158        std::fs::create_dir_all(&dir).unwrap();
1159        std::fs::write(dir.join("marker.txt"), "ok").unwrap();
1160        set_thread_execution_context(Some(RunExecutionRecord {
1161            cwd: Some(dir.to_string_lossy().into_owned()),
1162            env: BTreeMap::from([("HARN_PROCESS_TEST".to_string(), "present".to_string())]),
1163            ..Default::default()
1164        }));
1165        let output = exec_shell(
1166            None,
1167            "sh",
1168            "-c",
1169            "printf '%s:' \"$HARN_PROCESS_TEST\" && test -f marker.txt",
1170        )
1171        .unwrap();
1172        assert!(output.status.success());
1173        assert_eq!(String::from_utf8_lossy(&output.stdout), "present:");
1174        reset_process_state();
1175        let _ = std::fs::remove_dir_all(&dir);
1176    }
1177
1178    #[test]
1179    fn exec_at_resolves_relative_to_execution_cwd() {
1180        let dir = std::env::temp_dir().join(format!("harn-process-rel-{}", uuid::Uuid::now_v7()));
1181        std::fs::create_dir_all(dir.join("nested")).unwrap();
1182        std::fs::write(dir.join("nested").join("marker.txt"), "ok").unwrap();
1183        set_thread_execution_context(Some(RunExecutionRecord {
1184            cwd: Some(dir.to_string_lossy().into_owned()),
1185            ..Default::default()
1186        }));
1187        let output = exec_shell(Some("nested"), "sh", "-c", "test -f marker.txt").unwrap();
1188        assert!(output.status.success());
1189        reset_process_state();
1190        let _ = std::fs::remove_dir_all(&dir);
1191    }
1192
1193    #[test]
1194    fn runtime_paths_uses_configurable_state_roots() {
1195        let _runtime_paths_env_lock = crate::runtime_paths::test_env_lock()
1196            .lock()
1197            .unwrap_or_else(|poisoned| poisoned.into_inner());
1198        let _env_guard = RuntimePathsEnvGuard::capture();
1199        let base =
1200            std::env::temp_dir().join(format!("harn-process-runtime-{}", uuid::Uuid::now_v7()));
1201        std::fs::create_dir_all(&base).unwrap();
1202        std::env::set_var(crate::runtime_paths::HARN_STATE_DIR_ENV, ".custom-harn");
1203        std::env::set_var(crate::runtime_paths::HARN_RUN_DIR_ENV, ".custom-runs");
1204        std::env::set_var(
1205            crate::runtime_paths::HARN_WORKTREE_DIR_ENV,
1206            ".custom-worktrees",
1207        );
1208        set_thread_execution_context(Some(RunExecutionRecord {
1209            cwd: Some(base.to_string_lossy().into_owned()),
1210            ..Default::default()
1211        }));
1212
1213        let mut vm = crate::vm::Vm::new();
1214        register_process_builtins(&mut vm);
1215        let mut out = String::new();
1216        let builtin = vm
1217            .builtins
1218            .get("runtime_paths")
1219            .expect("runtime_paths builtin");
1220        let paths = match builtin(&[], &mut out).unwrap() {
1221            VmValue::Dict(map) => map,
1222            other => panic!("expected dict, got {other:?}"),
1223        };
1224        assert_eq!(
1225            paths.get("state_root").unwrap().display(),
1226            base.join(".custom-harn").display().to_string()
1227        );
1228        assert_eq!(
1229            paths.get("run_root").unwrap().display(),
1230            base.join(".custom-runs").display().to_string()
1231        );
1232        assert_eq!(
1233            paths.get("worktree_root").unwrap().display(),
1234            base.join(".custom-worktrees").display().to_string()
1235        );
1236
1237        reset_process_state();
1238        let _ = std::fs::remove_dir_all(&base);
1239    }
1240
1241    #[cfg(unix)]
1242    fn exec_opts_list(items: &[&str]) -> VmValue {
1243        VmValue::List(std::sync::Arc::new(
1244            items
1245                .iter()
1246                .map(|s| VmValue::String(std::sync::Arc::from(*s)))
1247                .collect(),
1248        ))
1249    }
1250
1251    #[cfg(unix)]
1252    fn exec_opts_dict(pairs: &[(&str, VmValue)]) -> VmValue {
1253        VmValue::dict(
1254            pairs
1255                .iter()
1256                .map(|(k, v)| (k.to_string(), v.clone()))
1257                .collect::<crate::value::DictMap>(),
1258        )
1259    }
1260
1261    #[cfg(unix)]
1262    #[test]
1263    fn exec_opts_merges_env_with_parent_by_default() {
1264        std::env::set_var("HARN_EXEC_OPTS_PARENT", "from-parent");
1265        let env = exec_opts_dict(&[("CHILD", VmValue::String(std::sync::Arc::from("from-child")))]);
1266        let args = vec![
1267            exec_opts_list(&[
1268                "/bin/sh",
1269                "-c",
1270                "printf '%s|%s' \"$HARN_EXEC_OPTS_PARENT\" \"$CHILD\"",
1271            ]),
1272            exec_opts_dict(&[("env", env)]),
1273        ];
1274        let mut out = String::new();
1275        let result = exec_opts_impl(&args, &mut out).expect("exec_opts result");
1276        let dict = result.as_dict().expect("dict");
1277        assert_eq!(
1278            dict.get("stdout").unwrap().display(),
1279            "from-parent|from-child"
1280        );
1281        assert!(matches!(dict.get("success"), Some(VmValue::Bool(true))));
1282        std::env::remove_var("HARN_EXEC_OPTS_PARENT");
1283    }
1284
1285    #[cfg(unix)]
1286    #[test]
1287    fn exec_opts_replace_env_clears_parent() {
1288        std::env::set_var("HARN_EXEC_OPTS_PARENT2", "from-parent");
1289        let env = exec_opts_dict(&[("CHILD", VmValue::String(std::sync::Arc::from("from-child")))]);
1290        let args = vec![
1291            exec_opts_list(&[
1292                "/bin/sh",
1293                "-c",
1294                "printf '%s|%s' \"$HARN_EXEC_OPTS_PARENT2\" \"$CHILD\"",
1295            ]),
1296            exec_opts_dict(&[
1297                ("env", env),
1298                ("env_mode", VmValue::String(std::sync::Arc::from("replace"))),
1299            ]),
1300        ];
1301        let mut out = String::new();
1302        let result = exec_opts_impl(&args, &mut out).expect("exec_opts result");
1303        let dict = result.as_dict().expect("dict");
1304        assert_eq!(dict.get("stdout").unwrap().display(), "|from-child");
1305        std::env::remove_var("HARN_EXEC_OPTS_PARENT2");
1306    }
1307
1308    #[cfg(unix)]
1309    #[test]
1310    fn exec_at_opts_honors_directory() {
1311        let dir = std::env::temp_dir().join(format!("harn-exec-opts-cwd-{}", uuid::Uuid::now_v7()));
1312        std::fs::create_dir_all(&dir).unwrap();
1313        let args = vec![
1314            VmValue::String(std::sync::Arc::from(dir.to_string_lossy().into_owned())),
1315            exec_opts_list(&["/bin/sh", "-c", "pwd -P"]),
1316        ];
1317        let mut out = String::new();
1318        let result = exec_at_opts_impl(&args, &mut out).expect("exec_at_opts result");
1319        let dict = result.as_dict().expect("dict");
1320        // macOS /tmp is a symlink to /private/tmp; canonicalize for comparison.
1321        let want = std::fs::canonicalize(&dir).unwrap();
1322        let got = dict.get("stdout").unwrap().display();
1323        assert_eq!(got.trim(), want.to_string_lossy());
1324        let _ = std::fs::remove_dir_all(&dir);
1325    }
1326
1327    #[cfg(unix)]
1328    #[test]
1329    fn exec_opts_enforces_timeout() {
1330        let args = vec![
1331            exec_opts_list(&["/bin/sh", "-c", "sleep 5"]),
1332            exec_opts_dict(&[("timeout", VmValue::Int(50))]),
1333        ];
1334        let mut out = String::new();
1335        let result = exec_opts_impl(&args, &mut out).expect("exec_opts result");
1336        let dict = result.as_dict().expect("dict");
1337        assert!(
1338            matches!(dict.get("timed_out"), Some(VmValue::Bool(true))),
1339            "command exceeding timeout must report timed_out"
1340        );
1341        assert!(matches!(dict.get("success"), Some(VmValue::Bool(false))));
1342    }
1343
1344    #[cfg(unix)]
1345    #[test]
1346    fn exec_opts_rejects_empty_command() {
1347        let args = vec![exec_opts_list(&[])];
1348        let mut out = String::new();
1349        assert!(exec_opts_impl(&args, &mut out).is_err());
1350        let bad = vec![VmValue::String(std::sync::Arc::from("not-a-list"))];
1351        assert!(exec_opts_impl(&bad, &mut out).is_err());
1352    }
1353}