harn-hostlib 0.10.39

Opt-in code-intelligence and deterministic-tool host builtins for the Harn VM
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Production [`ProcessSpawner`] implementation backed by
//! `std::process::Command` + `harn_vm::process_sandbox`.

use std::fs::OpenOptions;
use std::io::{self, Read, Write};
use std::process::{Child, ChildStderr, ChildStdin, ChildStdout, Command, Stdio};
use std::sync::{Arc, LazyLock};
use std::thread;
use std::time::{Duration, Instant};

use harn_vm::process_sandbox;

use super::handle::{
    EnvMode, ExitStatus, OutputCapture, ProcessCleanupReport, ProcessError, ProcessHandle,
    ProcessKiller, ProcessSpawner, SpawnSpec, WaitOutcome,
};

/// Spawner that produces real OS processes via `std::process::Command`.
pub struct RealSpawner;

static REAL_SPAWNER: LazyLock<Arc<dyn ProcessSpawner>> =
    LazyLock::new(|| Arc::new(RealSpawner) as Arc<dyn ProcessSpawner>);

/// Returns the singleton real spawner used as the default.
pub fn default_spawner() -> Arc<dyn ProcessSpawner> {
    Arc::clone(&REAL_SPAWNER)
}

impl ProcessSpawner for RealSpawner {
    fn spawn(&self, spec: SpawnSpec) -> Result<Box<dyn ProcessHandle>, ProcessError> {
        #[cfg(unix)]
        if spec.owner_death == super::OwnerDeathPolicy::KillContainment {
            if spec.use_stdin {
                return Err(ProcessError::InvalidArgv(
                    "owner-death containment reserves stdin for the liveness pipe".to_string(),
                ));
            }
            if !matches!(spec.output_capture, OutputCapture::Pipe) {
                return Err(ProcessError::InvalidArgv(
                    "owner-death containment requires piped output".to_string(),
                ));
            }
            if !spec.configure_process_group {
                return Err(ProcessError::InvalidArgv(
                    "owner-death containment requires an independent process group".to_string(),
                ));
            }
            let cleanup_token = harn_vm::op_interrupt::new_process_cleanup_token();
            let mut command = super::owner_death::prepare_guardian(&spec, cleanup_token.clone())?;
            let mut child = command.spawn().map_err(map_spawn_error)?;
            let liveness = child.stdin.take().ok_or_else(|| {
                ProcessError::Spawn("guardian liveness pipe was not created".to_string())
            })?;
            let (stderr, guardian_pid, payload_pid) =
                match super::owner_death::await_startup(&mut child) {
                    Ok(startup) => startup,
                    Err(error) => {
                        let _ = harn_vm::op_interrupt::signal_pid_tree_and_group_with_report(
                            child.id(),
                            9,
                        );
                        let _ = child.wait();
                        return Err(error);
                    }
                };
            return Ok(real_process(
                child,
                cleanup_token,
                Some(liveness),
                Some(stderr),
                Some(guardian_pid),
                Some(payload_pid),
                None,
            ));
        }

        let (mut command, cleanup_token) = prepare_command(&spec, None)?;
        #[cfg(target_os = "windows")]
        let owner_job = if spec.owner_death == super::OwnerDeathPolicy::KillContainment {
            let job = super::windows::KillOnCloseJob::new().map_err(|error| {
                ProcessError::Spawn(format!("create owner Job Object: {error}"))
            })?;
            super::windows::configure_suspended(&mut command);
            Some(Arc::new(job))
        } else {
            None
        };
        #[cfg(not(target_os = "windows"))]
        let owner_job = None;
        let child = command.spawn().map_err(map_spawn_error)?;

        #[cfg(target_os = "windows")]
        if let Some(job) = &owner_job {
            if let Err(error) = job
                .assign_process(child.id())
                .and_then(|()| super::windows::resume_process(child.id()))
            {
                let mut child = child;
                let _ = job.terminate();
                let _ = child.kill();
                let _ = child.wait();
                return Err(ProcessError::Spawn(format!(
                    "contain suspended worker in owner Job Object: {error}"
                )));
            }
        }

        Ok(real_process(
            child,
            cleanup_token,
            None,
            None,
            None,
            None,
            owner_job,
        ))
    }
}

pub(crate) fn prepare_command(
    spec: &SpawnSpec,
    cleanup_token: Option<String>,
) -> Result<(Command, String), ProcessError> {
    if spec.program.is_empty() {
        return Err(ProcessError::InvalidArgv(
            "first element of argv must be a non-empty program name".to_string(),
        ));
    }

    let mut command = process_sandbox::std_command_for(&spec.program, &spec.args)
        .map_err(|e| ProcessError::SandboxSetup(format!("{e:?}")))?;

    if let Some(cwd) = spec.cwd.as_ref() {
        process_sandbox::enforce_process_cwd(cwd)
            .map_err(|e| ProcessError::SandboxCwd(format!("{e:?}")))?;
        command.current_dir(cwd);
    }

    match spec.env_mode {
        // `Replace` starts from an empty environment, so nothing to strip.
        EnvMode::Replace => {
            command.env_clear();
        }
        // `InheritClean`/`Patch` inherit the full parent environment. Strip
        // secret-bearing variables (provider `*_API_KEY`s, `GITHUB_TOKEN`,
        // `HARN_CLOUD_API_KEY`, etc.) so build/test commands — and the model
        // that reads their stdout as the tool result — never see them.
        // Caller-supplied `env` below is applied afterward and is an
        // explicit opt-in, so it is intentionally not filtered here.
        EnvMode::InheritClean | EnvMode::Patch => {
            for (key, _) in std::env::vars_os() {
                if let Some(name) = key.to_str() {
                    if super::handle::is_sensitive_env_name(name) {
                        command.env_remove(&key);
                    }
                }
            }
        }
    }
    // Caller-requested inherited-env strips (e.g. a harness spawning a
    // child harn/burin process that must not write into the parent's
    // event-log or transcript dirs). Applied before `spec.env`, so an
    // explicitly supplied override still wins.
    for key in &spec.env_remove {
        command.env_remove(key);
    }
    for (key, value) in &spec.env {
        command.env(key, value);
    }

    // Give the child workspace-local temp, home, and toolchain-cache paths.
    // Applied after `spec.env`; caller-set keys win. The values are workspace
    // paths, not secrets, so this does not widen the scrub surface above.
    for (key, value) in process_sandbox::active_workspace_process_env() {
        if spec.env.contains_key(&key) {
            continue;
        }
        command.env(key, value);
    }

    // Pin tool *message* output to a deterministic English/UTF-8 locale so
    // downstream English-diagnostic matchers (deterministic syntax repair,
    // error-signature grounding, completion/pass-fail classification) do not
    // misfire for a non-Anglosphere user whose shell localizes compiler/test
    // output. A user-inherited `LC_ALL` overrides `LC_MESSAGES`, so strip it
    // first — unless the caller pinned it. Then apply the overlay with the
    // same caller-wins rule as the TMPDIR overlay above.
    if !spec
        .env
        .contains_key(process_sandbox::MESSAGE_LOCALE_OVERRIDE_ENV)
    {
        command.env_remove(process_sandbox::MESSAGE_LOCALE_OVERRIDE_ENV);
    }
    for (key, value) in process_sandbox::deterministic_message_locale_env() {
        if spec.env.contains_key(&key) {
            continue;
        }
        command.env(key, value);
    }

    log_spawn_context(&command, spec.env_mode);

    if spec.configure_process_group {
        configure_background_process_group(&mut command);
    }
    let cleanup_token =
        cleanup_token.unwrap_or_else(harn_vm::op_interrupt::new_process_cleanup_token);
    command.env(
        harn_vm::op_interrupt::PROCESS_CLEANUP_TOKEN_ENV,
        &cleanup_token,
    );

    match &spec.output_capture {
        OutputCapture::Inherit => {
            command.stdout(Stdio::inherit());
            command.stderr(Stdio::inherit());
        }
        OutputCapture::Pipe => {
            command.stdout(Stdio::piped());
            command.stderr(Stdio::piped());
        }
        OutputCapture::File {
            stdout_path,
            stderr_path,
        } => {
            let stdout = OpenOptions::new()
                .write(true)
                .truncate(true)
                .open(stdout_path)
                .map_err(|error| ProcessError::Spawn(format!("open stdout capture: {error}")))?;
            let stderr = OpenOptions::new()
                .write(true)
                .truncate(true)
                .open(stderr_path)
                .map_err(|error| ProcessError::Spawn(format!("open stderr capture: {error}")))?;
            command.stdout(Stdio::from(stdout));
            command.stderr(Stdio::from(stderr));
        }
    }
    command.stdin(match (&spec.output_capture, spec.use_stdin) {
        (OutputCapture::Inherit, true) => Stdio::inherit(),
        (_, true) => Stdio::piped(),
        (_, false) => Stdio::null(),
    });

    Ok((command, cleanup_token))
}

/// Record only the non-secret facts needed to diagnose command-resolution
/// failures. Arguments and the rest of the environment may contain credentials
/// or user data, so this boundary intentionally logs neither.
fn log_spawn_context(command: &Command, env_mode: EnvMode) {
    let program = command.get_program().to_string_lossy();
    let cwd = command
        .get_current_dir()
        .map(std::path::Path::to_path_buf)
        .or_else(|| std::env::current_dir().ok());
    let path = resolved_env_value(command, "PATH", env_mode)
        .map(|value| value.to_string_lossy().into_owned());
    tracing::debug!(
        target: "harn_hostlib::process",
        shell_or_program = %program,
        cwd = %cwd.as_deref().map_or_else(|| "<unresolved>".into(), std::path::Path::to_string_lossy),
        path = %path.as_deref().unwrap_or("<unset>"),
        env_mode = ?env_mode,
        "resolved command spawn context"
    );
}

fn resolved_env_value(
    command: &Command,
    name: &str,
    env_mode: EnvMode,
) -> Option<std::ffi::OsString> {
    for (key, value) in command.get_envs() {
        if env_key_eq(key, name) {
            return value.map(std::ffi::OsStr::to_os_string);
        }
    }
    if env_mode == EnvMode::Replace {
        None
    } else {
        std::env::var_os(name)
    }
}

fn env_key_eq(key: &std::ffi::OsStr, expected: &str) -> bool {
    #[cfg(windows)]
    {
        key.to_string_lossy().eq_ignore_ascii_case(expected)
    }
    #[cfg(not(windows))]
    {
        key == expected
    }
}

fn map_spawn_error(error: io::Error) -> ProcessError {
    if let Some(violation) = process_sandbox::process_spawn_error(&error) {
        return ProcessError::SandboxSpawn(format!("{violation:?}"));
    }
    ProcessError::Spawn(error.to_string())
}

/// Replace the current Unix process through the same prepared-command path as
/// normal hostlib spawns. A successful call never returns.
#[cfg(unix)]
pub fn replace_current_process(spec: SpawnSpec) -> Result<std::convert::Infallible, ProcessError> {
    use std::os::unix::process::CommandExt;

    super::handle::validate_process_spec(&spec)?;
    let inherited_cleanup_token = std::env::var(harn_vm::op_interrupt::PROCESS_CLEANUP_TOKEN_ENV)
        .ok()
        .filter(|token| !token.is_empty());
    let (mut command, _cleanup_token) = prepare_command(&spec, inherited_cleanup_token)?;
    Err(map_spawn_error(command.exec()))
}

struct RealProcess {
    pid: u32,
    pgid: Option<u32>,
    cleanup_token: String,
    killer: Arc<dyn ProcessKiller>,
    child: Option<Child>,
    stdin: Option<ChildStdin>,
    stdout: Option<ChildStdout>,
    stderr: Option<ChildStderr>,
    owner_liveness: Option<ChildStdin>,
    stdin_taken: bool,
    stdout_taken: bool,
    stderr_taken: bool,
}

fn real_process(
    child: Child,
    cleanup_token: String,
    owner_liveness: Option<ChildStdin>,
    stderr: Option<ChildStderr>,
    reported_pid: Option<u32>,
    killer_pid: Option<u32>,
    #[cfg(target_os = "windows")] owner_job: Option<Arc<super::windows::KillOnCloseJob>>,
    #[cfg(not(target_os = "windows"))] _owner_job: Option<()>,
) -> Box<dyn ProcessHandle> {
    let pid = reported_pid.unwrap_or_else(|| child.id());
    let pgid = child_process_group_id(pid);
    let killer: Arc<dyn ProcessKiller> = Arc::new(RealKiller {
        pid: killer_pid.unwrap_or(pid),
        cleanup_token: cleanup_token.clone(),
        #[cfg(target_os = "windows")]
        owner_job,
    });
    Box::new(RealProcess {
        pid,
        pgid,
        cleanup_token,
        killer,
        child: Some(child),
        stdin: None,
        stdout: None,
        stderr,
        owner_liveness,
        stdin_taken: false,
        stdout_taken: false,
        stderr_taken: false,
    })
}

impl RealProcess {
    fn ensure_pipes_taken(&mut self) {
        if let Some(child) = self.child.as_mut() {
            if self.owner_liveness.is_none() && self.stdin.is_none() && !self.stdin_taken {
                self.stdin = child.stdin.take();
            }
            if self.stdout.is_none() && !self.stdout_taken {
                self.stdout = child.stdout.take();
            }
            if self.stderr.is_none() && !self.stderr_taken {
                self.stderr = child.stderr.take();
            }
        }
    }
}

impl ProcessHandle for RealProcess {
    fn pid(&self) -> Option<u32> {
        Some(self.pid)
    }

    fn process_group_id(&self) -> Option<u32> {
        self.pgid
    }

    fn killer(&self) -> Arc<dyn ProcessKiller> {
        Arc::clone(&self.killer)
    }

    fn take_stdin(&mut self) -> Option<Box<dyn Write + Send>> {
        self.ensure_pipes_taken();
        self.stdin_taken = true;
        self.stdin
            .take()
            .map(|s| Box::new(s) as Box<dyn Write + Send>)
    }

    fn take_stdout(&mut self) -> Option<Box<dyn Read + Send>> {
        self.ensure_pipes_taken();
        self.stdout_taken = true;
        self.stdout
            .take()
            .map(|s| Box::new(s) as Box<dyn Read + Send>)
    }

    fn take_stderr(&mut self) -> Option<Box<dyn Read + Send>> {
        self.ensure_pipes_taken();
        self.stderr_taken = true;
        self.stderr
            .take()
            .map(|s| Box::new(s) as Box<dyn Read + Send>)
    }

    fn wait_with_timeout(
        &mut self,
        timeout: Option<Duration>,
        interrupt: &dyn Fn() -> bool,
    ) -> io::Result<WaitOutcome> {
        let killer = Arc::clone(&self.killer);
        let owner_death_contained = self.owner_liveness.is_some();
        let Some(child) = self.child.as_mut() else {
            return Err(io::Error::other("child already reaped"));
        };
        let deadline = timeout.map(|timeout| Instant::now() + timeout);
        loop {
            match child.try_wait()? {
                Some(status) => return Ok(WaitOutcome::Exited(decode_status(status))),
                None => {
                    if interrupt() {
                        if owner_death_contained {
                            let report = killer.kill();
                            let _ = child.wait();
                            return Ok(WaitOutcome::Interrupted(report));
                        }
                        // Scope cancellation / deadline expiry: graceful
                        // group termination (SIGTERM, grace, SIGKILL) shared
                        // with the VM-side `process.*` builtins.
                        let (_, report) =
                            harn_vm::op_interrupt::terminate_child_group_with_cleanup_token_report(
                                child,
                                Some(&self.cleanup_token),
                            );
                        return Ok(WaitOutcome::Interrupted(report));
                    }
                    if deadline.is_some_and(|deadline| Instant::now() >= deadline) {
                        // `killer.kill()` kills the process tree/group on
                        // Unix. That path is a no-op on non-Unix targets, so
                        // also kill the child handle directly
                        // (TerminateProcess on Windows) to guarantee the
                        // subsequent `child.wait()` cannot block forever on a
                        // timed-out process.
                        let mut report = killer.kill();
                        if !owner_death_contained {
                            let _ = child.kill();
                        }
                        let _ = child.wait();
                        report.refresh_survivor_status();
                        return Ok(WaitOutcome::TimedOut(report));
                    }
                    let sleep = deadline
                        .map(|deadline| deadline.saturating_duration_since(Instant::now()))
                        .unwrap_or(Duration::MAX)
                        .min(Duration::from_millis(20));
                    thread::sleep(sleep);
                }
            }
        }
    }

    fn wait(&mut self) -> io::Result<ExitStatus> {
        let child = self
            .child
            .as_mut()
            .ok_or_else(|| io::Error::other("child already reaped"))?;
        let status = child.wait()?;
        Ok(decode_status(status))
    }
}

struct RealKiller {
    pid: u32,
    cleanup_token: String,
    #[cfg(target_os = "windows")]
    owner_job: Option<Arc<super::windows::KillOnCloseJob>>,
}

impl ProcessKiller for RealKiller {
    fn kill(&self) -> ProcessCleanupReport {
        let report = harn_vm::op_interrupt::signal_pid_tree_group_and_token_with_report(
            self.pid,
            Some(&self.cleanup_token),
            9,
        );
        #[cfg(target_os = "windows")]
        if let Some(job) = &self.owner_job {
            let _ = job.terminate();
        } else {
            terminate_process(self.pid);
        }
        report
    }
}

#[cfg(target_os = "windows")]
fn terminate_process(pid: u32) {
    use windows_sys::Win32::Foundation::CloseHandle;
    use windows_sys::Win32::System::Threading::{OpenProcess, TerminateProcess, PROCESS_TERMINATE};

    let handle = unsafe { OpenProcess(PROCESS_TERMINATE, 0, pid) };
    if handle.is_null() {
        return;
    }
    unsafe {
        TerminateProcess(handle, 1);
        CloseHandle(handle);
    }
}

#[cfg(unix)]
fn decode_status(status: std::process::ExitStatus) -> ExitStatus {
    use std::os::unix::process::ExitStatusExt;
    if let Some(code) = status.code() {
        ExitStatus::from_code(code)
    } else if let Some(sig) = status.signal() {
        ExitStatus::from_signal(sig)
    } else {
        ExitStatus {
            code: None,
            signal: None,
        }
    }
}

#[cfg(not(unix))]
fn decode_status(status: std::process::ExitStatus) -> ExitStatus {
    ExitStatus::from_code(status.code().unwrap_or(-1))
}

pub(crate) fn child_process_group_id(pid: u32) -> Option<u32> {
    #[cfg(unix)]
    {
        extern "C" {
            fn getpgid(pid: i32) -> i32;
        }
        let pgid = unsafe { getpgid(pid as i32) };
        if pgid > 0 {
            Some(pgid as u32)
        } else {
            None
        }
    }
    #[cfg(not(unix))]
    {
        Some(pid)
    }
}

pub(crate) fn configure_background_process_group(command: &mut std::process::Command) {
    #[cfg(unix)]
    {
        use std::os::unix::process::CommandExt;
        command.process_group(0);
    }
    #[cfg(not(unix))]
    {
        let _ = command;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn resolved_path_prefers_the_child_override() {
        let mut command = Command::new("shell");
        command.env("PATH", "/resolved/toolchain/bin");

        assert_eq!(
            resolved_env_value(&command, "PATH", EnvMode::Patch),
            Some(std::ffi::OsString::from("/resolved/toolchain/bin"))
        );
    }

    #[test]
    fn resolved_path_honors_an_explicit_removal() {
        let mut command = Command::new("shell");
        command.env_remove("PATH");

        assert_eq!(resolved_env_value(&command, "PATH", EnvMode::Patch), None);
    }

    #[test]
    fn replace_mode_does_not_report_an_inherited_path() {
        let command = Command::new("shell");

        assert_eq!(resolved_env_value(&command, "PATH", EnvMode::Replace), None);
    }
}