oxdock-process 0.14.0-alpha

Process orchestration for OxDock environments.
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
// Synthetic process manager for Miri. Commands are interpreted with a tiny
// shell that supports the patterns exercised in tests: sleep, printf/echo with
// env interpolation, redirection, and exit codes. IO is routed through the
// workspace filesystem so we never touch the host.
use anyhow::{Result, bail};
#[allow(clippy::disallowed_types, clippy::disallowed_methods)]
use std::process::ExitStatus;

use oxdock_fs::{GuardedPath, PathResolver, PolicyPath};
use oxdock_sys_test_utils::exit_status_from_code;

use crate::contract::{
    BackgroundHandle, CommandContext, CommandMode, CommandOptions, CommandResult, CommandStderr,
    CommandStdin, CommandStdout, ProcessManager,
};
use crate::expand::expand_with_lookup;

#[cfg(miri)]
#[derive(Clone, Default)]
pub struct SyntheticProcessManager;

#[cfg(miri)]
#[derive(Clone)]
pub struct SyntheticBgHandle {
    ctx: CommandContext,
    actions: Vec<Action>,
    remaining: std::time::Duration,
    last_polled: std::time::Instant,
    status: ExitStatus,
    applied: bool,
    killed: bool,
}

#[cfg(miri)]
#[derive(Clone)]
enum Action {
    WriteFile { target: GuardedPath, data: Vec<u8> },
}

#[cfg(miri)]
impl BackgroundHandle for SyntheticBgHandle {
    fn try_wait(&mut self) -> Result<Option<ExitStatus>> {
        if self.killed {
            self.applied = true;
            return Ok(Some(self.status));
        }
        if self.applied {
            return Ok(Some(self.status));
        }
        let now = std::time::Instant::now();
        let elapsed = now.saturating_duration_since(self.last_polled);
        const MAX_ADVANCE: std::time::Duration = std::time::Duration::from_millis(15);
        let advance = elapsed.min(MAX_ADVANCE).min(self.remaining);
        self.remaining = self.remaining.saturating_sub(advance);
        self.last_polled = now;

        if self.remaining.is_zero() {
            apply_actions(&self.ctx, &self.actions)?;
            self.applied = true;
            Ok(Some(self.status))
        } else {
            Ok(None)
        }
    }

    fn kill(&mut self) -> Result<()> {
        self.killed = true;
        Ok(())
    }

    fn wait(&mut self) -> Result<ExitStatus> {
        if self.killed {
            self.applied = true;
            return Ok(self.status);
        }
        if !self.applied {
            if !self.remaining.is_zero() {
                std::thread::sleep(self.remaining);
            }
            apply_actions(&self.ctx, &self.actions)?;
            self.applied = true;
        }
        Ok(self.status)
    }
}

#[cfg(miri)]
impl Drop for SyntheticBgHandle {
    fn drop(&mut self) {
        self.killed = true;
    }
}

#[cfg(miri)]
impl ProcessManager for SyntheticProcessManager {
    type Handle = SyntheticBgHandle;

    fn run_command(
        &mut self,
        ctx: &CommandContext,
        script: &str,
        options: CommandOptions,
    ) -> Result<CommandResult<Self::Handle>> {
        let CommandOptions {
            mode,
            stdin,
            stdout,
            stderr,
        } = options;

        if let CommandStdin::Stream(reader) = &stdin
            && let Ok(mut guard) = reader.lock()
        {
            let mut sink = std::io::sink();
            let _ = std::io::copy(&mut *guard, &mut sink);
        }

        match mode {
            CommandMode::Foreground => {
                let needs_bytes = matches!(stdout, CommandStdout::Capture)
                    || matches!(stdout, CommandStdout::Stream(_));
                let (out, status) = execute_sync(ctx, script, needs_bytes)?;
                if !status.success() {
                    bail!("command {:?} failed with status {}", script, status);
                }
                if matches!(stderr, CommandStderr::Stream(_)) {
                    // Synthetic manager does not produce stderr output; warn if requested.
                    // We simply ignore the stream since no bytes are generated.
                }
                match stdout {
                    CommandStdout::Inherit => Ok(CommandResult::Completed),
                    CommandStdout::Stream(writer) => {
                        if needs_bytes && let Ok(mut guard) = writer.lock() {
                            let _ = std::io::Write::write_all(&mut *guard, &out);
                            let _ = std::io::Write::flush(&mut *guard);
                        }
                        Ok(CommandResult::Completed)
                    }
                    CommandStdout::Capture => Ok(CommandResult::Captured(out)),
                }
            }
            CommandMode::Background => match stdout {
                CommandStdout::Capture => {
                    bail!("cannot capture stdout for background command under miri")
                }
                CommandStdout::Stream(_) => {
                    bail!("stdout streaming not supported for background command under miri")
                }
                CommandStdout::Inherit => {
                    if matches!(stderr, CommandStderr::Stream(_)) {
                        bail!("stderr streaming not supported for background command under miri");
                    }
                    let plan = plan_background(ctx, script)?;
                    Ok(CommandResult::Background(plan))
                }
            },
        }
    }

    fn run_argv(
        &mut self,
        ctx: &CommandContext,
        argv: &[String],
        options: CommandOptions,
    ) -> Result<CommandResult<Self::Handle>> {
        let CommandOptions {
            mode,
            stdin,
            stdout,
            stderr,
        } = options;

        if let CommandStdin::Stream(reader) = &stdin
            && let Ok(mut guard) = reader.lock()
        {
            let mut sink = std::io::sink();
            let _ = std::io::copy(&mut *guard, &mut sink);
        }

        if argv.is_empty() {
            bail!("RUN exec form requires at least one argument");
        }

        match mode {
            CommandMode::Foreground => {
                let needs_bytes = matches!(stdout, CommandStdout::Capture)
                    || matches!(stdout, CommandStdout::Stream(_));
                let (out, status) = execute_argv_sync(ctx, argv, needs_bytes)?;
                if !status.success() {
                    bail!("command {argv:?} failed with status {}", status);
                }
                if matches!(stderr, CommandStderr::Stream(_)) {
                    // Synthetic manager does not produce stderr output.
                }
                match stdout {
                    CommandStdout::Inherit => Ok(CommandResult::Completed),
                    CommandStdout::Stream(writer) => {
                        if needs_bytes && let Ok(mut guard) = writer.lock() {
                            let _ = std::io::Write::write_all(&mut *guard, &out);
                            let _ = std::io::Write::flush(&mut *guard);
                        }
                        Ok(CommandResult::Completed)
                    }
                    CommandStdout::Capture => Ok(CommandResult::Captured(out)),
                }
            }
            CommandMode::Background => match stdout {
                CommandStdout::Capture => {
                    bail!("cannot capture stdout for background command under miri")
                }
                CommandStdout::Stream(_) => {
                    bail!("stdout streaming not supported for background command under miri")
                }
                CommandStdout::Inherit => {
                    if matches!(stderr, CommandStderr::Stream(_)) {
                        bail!("stderr streaming not supported for background command under miri");
                    }
                    let plan = plan_argv_background(ctx, argv)?;
                    Ok(CommandResult::Background(plan))
                }
            },
        }
    }
}

/// Direct-spawn `argv` execution for Miri: dispatches on `argv[0]` without
/// shell parsing (`sleep`/`exit`/`echo`/`printf`, else no-op success).
/// Stdout bytes only cover the `echo`/`printf` foreground-capture path;
/// argv form has no shell redirections, so no file writes occur here.
#[cfg(miri)]
fn execute_argv_sync(
    _ctx: &CommandContext,
    argv: &[String],
    capture: bool,
) -> Result<(Vec<u8>, ExitStatus)> {
    let mut stdout = Vec::new();
    let mut status = exit_status_from_code(0);
    match argv[0].as_str() {
        "sleep" => {
            let dur = argv
                .get(1)
                .and_then(|s| s.parse::<f64>().ok())
                .unwrap_or(0.0);
            std::thread::sleep(std::time::Duration::from_secs_f64(dur));
        }
        "exit" => {
            let code = argv.get(1).and_then(|s| s.parse::<i32>().ok()).unwrap_or(0);
            status = exit_status_from_code(code);
        }
        "echo" => {
            if capture {
                let mut data = argv[1..].join(" ").into_bytes();
                data.push(b'\n');
                stdout.extend_from_slice(&data);
            }
        }
        "printf" if capture => {
            let rest = if argv.get(1).is_some_and(|s| s == "%s") {
                &argv[2..]
            } else {
                &argv[1..]
            };
            stdout.extend_from_slice(rest.join(" ").as_bytes());
        }
        _ => {}
    }
    Ok((stdout, status))
}

#[cfg(miri)]
fn plan_argv_background(ctx: &CommandContext, argv: &[String]) -> Result<SyntheticBgHandle> {
    let mut ready = std::time::Duration::ZERO;
    let mut status = exit_status_from_code(0);
    match argv[0].as_str() {
        "sleep" => {
            let dur = argv
                .get(1)
                .and_then(|s| s.parse::<f64>().ok())
                .unwrap_or(0.0);
            ready += std::time::Duration::from_secs_f64(dur);
        }
        "exit" => {
            let code = argv.get(1).and_then(|s| s.parse::<i32>().ok()).unwrap_or(0);
            status = exit_status_from_code(code);
        }
        _ => {}
    }
    let min_ready = std::time::Duration::from_millis(50);
    ready = ready.max(min_ready);
    Ok(SyntheticBgHandle {
        ctx: ctx.clone(),
        actions: Vec::new(),
        remaining: ready,
        last_polled: std::time::Instant::now(),
        status,
        applied: false,
        killed: false,
    })
}

#[cfg(miri)]
fn execute_sync(
    ctx: &CommandContext,
    script: &str,
    capture: bool,
) -> Result<(Vec<u8>, ExitStatus)> {
    let mut stdout = Vec::new();
    let mut status = exit_status_from_code(0);
    let resolver = PathResolver::new(
        ctx.workspace_root().as_path(),
        ctx.build_context().as_path(),
    )?;

    let script = normalize_shell(script);
    for raw in script.split(';') {
        let cmd = raw.trim();
        if cmd.is_empty() {
            continue;
        }
        let (action, sleep_dur, exit_code) = parse_command(cmd, ctx, &resolver, capture)?;
        if sleep_dur > std::time::Duration::ZERO {
            std::thread::sleep(sleep_dur);
        }
        if let Some(action) = action {
            match action {
                CommandAction::Write { target, data } => {
                    if let Some(parent) = target.as_path().parent() {
                        let parent_guard = GuardedPath::new(target.root(), parent)?;
                        resolver.create_dir_all(&parent_guard)?;
                    }
                    resolver.write_file(&target, &data)?;
                }
                CommandAction::Stdout { data } => {
                    stdout.extend_from_slice(&data);
                }
            }
        }
        if let Some(code) = exit_code {
            status = exit_status_from_code(code);
            break;
        }
    }

    Ok((stdout, status))
}

#[cfg(miri)]
fn plan_background(ctx: &CommandContext, script: &str) -> Result<SyntheticBgHandle> {
    let resolver = PathResolver::new(
        ctx.workspace_root().as_path(),
        ctx.build_context().as_path(),
    )?;
    let mut actions: Vec<Action> = Vec::new();
    let mut ready = std::time::Duration::ZERO;
    let mut status = exit_status_from_code(0);

    let script = normalize_shell(script);
    for raw in script.split(';') {
        let cmd = raw.trim();
        if cmd.is_empty() {
            continue;
        }
        let (action, sleep_dur, exit_code) = parse_command(cmd, ctx, &resolver, false)?;
        ready += sleep_dur;
        if let Some(CommandAction::Write { target, data }) = action {
            actions.push(Action::WriteFile { target, data });
        }
        if let Some(code) = exit_code {
            status = exit_status_from_code(code);
            break;
        }
    }

    let min_ready = std::time::Duration::from_millis(50);
    ready = ready.max(min_ready);

    let handle = SyntheticBgHandle {
        ctx: ctx.clone(),
        actions,
        remaining: ready,
        last_polled: std::time::Instant::now(),
        status,
        applied: false,
        killed: false,
    };
    Ok(handle)
}

#[cfg(miri)]
enum CommandAction {
    Write { target: GuardedPath, data: Vec<u8> },
    Stdout { data: Vec<u8> },
}

#[cfg(miri)]
fn parse_command(
    cmd: &str,
    ctx: &CommandContext,
    resolver: &PathResolver,
    capture: bool,
) -> Result<(Option<CommandAction>, std::time::Duration, Option<i32>)> {
    let (core, redirect) = split_redirect(cmd);
    let tokens: Vec<&str> = core.split_whitespace().collect();
    if tokens.is_empty() {
        return Ok((None, std::time::Duration::ZERO, None));
    }

    match tokens[0] {
        "sleep" => {
            let dur = tokens
                .get(1)
                .and_then(|s| s.parse::<f64>().ok())
                .unwrap_or(0.0);
            let duration = std::time::Duration::from_secs_f64(dur);
            Ok((None, duration, None))
        }
        "exit" => {
            let code = tokens
                .get(1)
                .and_then(|s| s.parse::<i32>().ok())
                .unwrap_or(0);
            Ok((None, std::time::Duration::ZERO, Some(code)))
        }
        "printf" => {
            let body = extract_body(&core, "printf %s");
            let expanded = expand_env(&body, ctx);
            let data = expanded.into_bytes();
            if let Some(path_str) = redirect {
                let target = resolve_write(resolver, ctx, &path_str)?;
                Ok((
                    Some(CommandAction::Write { target, data }),
                    std::time::Duration::ZERO,
                    None,
                ))
            } else if capture {
                Ok((
                    Some(CommandAction::Stdout { data }),
                    std::time::Duration::ZERO,
                    None,
                ))
            } else {
                Ok((None, std::time::Duration::ZERO, None))
            }
        }
        "echo" => {
            let body = core.strip_prefix("echo").unwrap_or("").trim();
            let expanded = expand_env(body, ctx);
            let mut data = expanded.into_bytes();
            data.push(b'\n');
            if let Some(path_str) = redirect {
                let target = resolve_write(resolver, ctx, &path_str)?;
                Ok((
                    Some(CommandAction::Write { target, data }),
                    std::time::Duration::ZERO,
                    None,
                ))
            } else if capture {
                Ok((
                    Some(CommandAction::Stdout { data }),
                    std::time::Duration::ZERO,
                    None,
                ))
            } else {
                Ok((None, std::time::Duration::ZERO, None))
            }
        }
        _ => {
            // Fallback: treat as no-op success so Miri tests can proceed.
            Ok((None, std::time::Duration::ZERO, None))
        }
    }
}

#[cfg(miri)]
fn resolve_write(resolver: &PathResolver, ctx: &CommandContext, path: &str) -> Result<GuardedPath> {
    match ctx.cwd() {
        PolicyPath::Guarded(p) => resolver.resolve_write(p, path),
        PolicyPath::Unguarded(_) => bail!("unguarded writes not supported in Miri"),
    }
}

#[cfg(miri)]
fn split_redirect(cmd: &str) -> (String, Option<String>) {
    if let Some(idx) = cmd.find('>') {
        let (left, right) = cmd.split_at(idx);
        let path = right.trim_start_matches('>').trim();
        (left.trim().to_string(), Some(path.to_string()))
    } else {
        (cmd.trim().to_string(), None)
    }
}

#[cfg(miri)]
fn extract_body(cmd: &str, prefix: &str) -> String {
    cmd.strip_prefix(prefix)
        .unwrap_or(cmd)
        .trim()
        .trim_matches('"')
        .to_string()
}

#[cfg(miri)]
fn expand_env(input: &str, ctx: &CommandContext) -> String {
    // First expand any double-brace names ({{ ... }}), then expand simple
    // shell-style `$VAR` and `${VAR}` occurrences so the synthetic Miri
    // process emulation behaves like a real shell with respect to env vars.
    let first = expand_with_lookup(input, |name| Some(env_lookup(name, ctx)));

    let mut out = String::with_capacity(first.len());
    let mut chars = first.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '$' {
            match chars.peek() {
                Some('$') => {
                    // Preserve literal $$ (never a PID expansion here)
                    out.push_str("$$");
                    chars.next();
                }
                Some('{') => {
                    // ${VAR}
                    chars.next(); // consume '{'
                    let mut name = String::new();
                    while let Some(&ch) = chars.peek() {
                        chars.next();
                        if ch == '}' {
                            break;
                        }
                        name.push(ch);
                    }
                    let val = if name == "CARGO_TARGET_DIR" {
                        ctx.cargo_target_dir().display().to_string()
                    } else {
                        ctx.envs()
                            .get(&name)
                            .cloned()
                            .or_else(|| std::env::var(&name).ok())
                            .unwrap_or_default()
                    };
                    out.push_str(&val);
                }
                Some(next) if next.is_alphanumeric() || *next == '_' => {
                    // $VAR
                    let mut name = String::new();
                    while let Some(&ch) = chars.peek() {
                        if ch.is_alphanumeric() || ch == '_' {
                            name.push(ch);
                            chars.next();
                        } else {
                            break;
                        }
                    }
                    let val = if name == "CARGO_TARGET_DIR" {
                        ctx.cargo_target_dir().display().to_string()
                    } else {
                        ctx.envs()
                            .get(&name)
                            .cloned()
                            .or_else(|| std::env::var(&name).ok())
                            .unwrap_or_default()
                    };
                    out.push_str(&val);
                }
                _ => {
                    // Not a recognized var form; keep literal '$'
                    out.push('$');
                }
            }
        } else {
            out.push(c);
        }
    }

    out
}

#[cfg(miri)]
fn env_lookup(name: &str, ctx: &CommandContext) -> String {
    // Accept both `{{ env:X }}` and bare `{{ X }}` forms, matching the
    // host-side `expand_command_env` semantics.
    let key = name.strip_prefix("env:").unwrap_or(name);
    if key == "CARGO_TARGET_DIR" {
        return ctx.cargo_target_dir().display().to_string();
    }
    ctx.envs()
        .get(key)
        .cloned()
        .or_else(|| std::env::var(key).ok())
        .unwrap_or_default()
}

#[cfg(miri)]
fn normalize_shell(script: &str) -> String {
    let trimmed = script.trim();
    if let Some(rest) = trimmed.strip_prefix("sh -c ") {
        return rest.trim_matches(&['"', '\''] as &[_]).to_string();
    }
    if let Some(rest) = trimmed.strip_prefix("cmd /C ") {
        return rest.trim_matches(&['"', '\''] as &[_]).to_string();
    }
    trimmed.to_string()
}

#[cfg(miri)]
fn apply_actions(ctx: &CommandContext, actions: &[Action]) -> Result<()> {
    let resolver = PathResolver::new(
        ctx.workspace_root().as_path(),
        ctx.build_context().as_path(),
    )?;
    for action in actions {
        match action {
            Action::WriteFile { target, data } => {
                if let Some(parent) = target.as_path().parent() {
                    let parent_guard = GuardedPath::new(target.root(), parent)?;
                    resolver.create_dir_all(&parent_guard)?;
                }
                resolver.write_file(target, data)?;
            }
        }
    }
    Ok(())
}

#[cfg(miri)]
pub(crate) fn synthetic_status(snapshot: &crate::builder::CommandSnapshot) -> Result<ExitStatus> {
    Ok(synthetic_output(snapshot)?.status)
}

#[cfg(miri)]
pub(crate) fn synthetic_output(
    snapshot: &crate::builder::CommandSnapshot,
) -> Result<crate::builder::CommandOutput> {
    use crate::builder::CommandOutput;

    let program = snapshot.program.to_string_lossy().to_string();
    let args: Vec<String> = snapshot
        .args
        .iter()
        .map(|a| a.to_string_lossy().to_string())
        .collect();

    if program == "git" {
        return simulate_git(&args);
    }
    if program == "cargo" {
        return simulate_cargo(&args);
    }

    Ok(CommandOutput {
        status: exit_status_from_code(0),
        stdout: Vec::new(),
        stderr: Vec::new(),
    })
}

#[cfg(miri)]
fn simulate_git(args: &[String]) -> Result<crate::builder::CommandOutput> {
    use crate::builder::CommandOutput;

    let mut iter = args.iter();
    if matches!(iter.next(), Some(arg) if arg == "-C") {
        let _ = iter.next();
    }
    let remaining: Vec<String> = iter.map(|s| s.to_string()).collect();

    if remaining.len() >= 2 && remaining[0] == "rev-parse" && remaining[1] == "HEAD" {
        return Ok(CommandOutput {
            status: exit_status_from_code(0),
            stdout: b"HEAD\n".to_vec(),
            stderr: Vec::new(),
        });
    }

    // Default success for init/add/commit and other read-only queries.
    Ok(CommandOutput {
        status: exit_status_from_code(0),
        stdout: Vec::new(),
        stderr: Vec::new(),
    })
}

#[cfg(miri)]
fn simulate_cargo(args: &[String]) -> Result<crate::builder::CommandOutput> {
    use crate::builder::CommandOutput;

    // Heuristic: manifests containing "build_exit_fail" should fail to mimic fixture.
    let mut status = exit_status_from_code(0);
    if args.iter().any(|a| a.contains("build_exit_fail")) {
        status = exit_status_from_code(1);
    }
    Ok(CommandOutput {
        status,
        stdout: Vec::new(),
        stderr: Vec::new(),
    })
}

// Unit tests for the Miri-only synthetic process backend. This module only
// compiles under Miri, where the CI `cargo miri test` job exercises it.
#[cfg(all(miri, test))]
mod synthetic_backend_tests {
    use super::*;
    use std::collections::HashMap;

    fn miri_ctx(envs: &[(&str, &str)]) -> (GuardedPath, CommandContext) {
        let temp = GuardedPath::tempdir().expect("tempdir");
        let guard = temp.as_guarded_path().clone();
        let cwd: PolicyPath = guard.clone().into();
        let map: HashMap<String, String> = envs
            .iter()
            .map(|(key, value)| (key.to_string(), value.to_string()))
            .collect();
        let scratch = oxdock_fs::reserve_cargo_scratch().expect("scratch");
        let ctx = CommandContext::from_map(&cwd, &map, &scratch, &guard, &guard);
        (guard, ctx)
    }

    #[test]
    fn split_redirect_separates_target_and_trims() {
        assert_eq!(
            split_redirect("echo hi > out.txt"),
            ("echo hi".to_string(), Some("out.txt".to_string()))
        );
        assert_eq!(
            split_redirect("echo hi >> app.txt"),
            ("echo hi".to_string(), Some("app.txt".to_string()))
        );
        assert_eq!(split_redirect("  echo hi  "), ("echo hi".to_string(), None));
    }

    #[test]
    fn normalize_shell_strips_platform_wrappers() {
        assert_eq!(normalize_shell("sh -c \"echo hi\""), "echo hi");
        assert_eq!(normalize_shell("cmd /C \"echo hi\""), "echo hi");
        assert_eq!(normalize_shell("  echo hi "), "echo hi");
    }

    #[test]
    fn expand_env_supports_brace_and_dollar_forms() {
        let (_root, ctx) = miri_ctx(&[("FOO", "bar")]);

        assert_eq!(expand_env("{{ env:FOO }}!", &ctx), "bar!");
        assert_eq!(expand_env("$FOO!", &ctx), "bar!");
        assert_eq!(expand_env("${FOO}!", &ctx), "bar!");
        assert_eq!(expand_env("cost $$5", &ctx), "cost $$5");
        assert_eq!(expand_env("[${MISSING_XYZ}]", &ctx), "[]");

        let target = expand_env("${CARGO_TARGET_DIR}", &ctx);
        assert!(!target.is_empty(), "executor default must resolve");
    }

    #[test]
    fn parse_command_interprets_sleep_exit_and_echo() {
        let (_root, ctx) = miri_ctx(&[]);
        let resolver = PathResolver::new(
            ctx.workspace_root().as_path(),
            ctx.build_context().as_path(),
        )
        .expect("resolver");

        let (action, duration, code) =
            parse_command("sleep 0.05", &ctx, &resolver, false).expect("sleep");
        assert!(action.is_none());
        assert_eq!(duration, std::time::Duration::from_millis(50));
        assert!(code.is_none());

        let (action, _, code) = parse_command("exit 7", &ctx, &resolver, false).expect("exit");
        assert!(action.is_none());
        assert_eq!(code, Some(7));

        let (action, _, _) = parse_command("echo hi", &ctx, &resolver, true).expect("echo");
        match action {
            Some(CommandAction::Stdout { data }) => assert_eq!(data, b"hi\n"),
            _ => panic!("expected stdout action for captured echo"),
        }

        // Without capture, echo is a no-op.
        let (action, _, _) = parse_command("echo hi", &ctx, &resolver, false).expect("echo quiet");
        assert!(action.is_none());
    }

    #[test]
    fn parse_command_unknown_commands_are_noop_success() {
        let (_root, ctx) = miri_ctx(&[]);
        let resolver = PathResolver::new(
            ctx.workspace_root().as_path(),
            ctx.build_context().as_path(),
        )
        .expect("resolver");

        let (action, duration, code) =
            parse_command("cargo build --release", &ctx, &resolver, true).expect("unknown");
        assert!(action.is_none());
        assert_eq!(duration, std::time::Duration::ZERO);
        assert!(code.is_none());
    }

    #[test]
    fn parse_command_printf_redirects_write_into_workspace() {
        let (_root, ctx) = miri_ctx(&[]);
        let resolver = PathResolver::new(
            ctx.workspace_root().as_path(),
            ctx.build_context().as_path(),
        )
        .expect("resolver");

        let (action, _, _) = parse_command(
            "printf %s \"file-body\" > nested/out.txt",
            &ctx,
            &resolver,
            false,
        )
        .expect("redirect write");

        match action {
            Some(CommandAction::Write { target, data }) => {
                assert_eq!(data, b"file-body");
                assert!(target.as_path().starts_with(ctx.workspace_root().as_path()));
                assert!(target.as_path().ends_with("nested/out.txt"));
            }
            _ => panic!("expected write action"),
        }
    }
}