oxdock-process 0.45.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
//! Central host-injection policy: the single place deciding what may flow
//! from the host OS into scripts and spawned children.
//!
//! Mirror-or-absent world, stated once: session start copies host values
//! for the declared names directly into the script env map
//! ([`seed_defaults`]); a host lacking a name leaves it absent, never
//! synthesized. Explicit script `ENV`/`INHERIT_ENV` overwrite entries
//! uniformly; `ISOLATED` resets the map to the same seed. Missing values
//! surface at spawn time through [`check_spawn_anchors`], which names the
//! `INHERIT_ENV` fix: a missing host value is a host problem, reported,
//! never papered over.
//!
//! Names are declared once in the `define_env_contract!` invocation
//! below with their platforms. Doc tables, seed maps, and tests all derive
//! from that declaration, so there is no second list to drift.
//!
//! Every spawn site stages through here instead of hand-rolling its
//! own overlay and diagnostics, so the rule cannot drift per call site.
//!
//! Rendered reference: `ENV_CONTRACTS.md` at the repository root
//! (generated from the declaration below by `cargo run -p oxdock-render`;
//! templates link it through the `env_contracts_url` global).

use oxdock_diagnostics::DiagnosticResult;
use oxdock_fs::env as oxdock_env;
use std::collections::HashMap;

use crate::diagnostics as proc_diag;

/// Look up `name` in a script env map with OS comparison semantics:
/// case-insensitive on Windows (where `Path` and `PATH` name the same
/// variable), exact everywhere else.
pub fn has_var(envs: &HashMap<String, String>, name: &str) -> bool {
    #[allow(clippy::disallowed_macros)]
    if cfg!(windows) {
        envs.keys().any(|k| k.eq_ignore_ascii_case(name))
    } else {
        envs.contains_key(name)
    }
}

/// Whether `name` is a sanctioned ambient seed into initial script env:
/// the complete list of host names admitted without an explicit
/// `INHERIT_ENV` directive. Everything else arrives only by routing.
pub fn is_sanctioned_seed(name: &str) -> bool {
    name.starts_with("CARGO_FEATURE_") || name.starts_with("CARGO_CFG_")
}

/// Whether the OS consults `PATH` to resolve `program`: bare names do,
/// absolute paths and separator-carrying relative paths do not.
pub fn needs_path_lookup(program: &str) -> bool {
    !program.contains('/') && !program.contains('\\')
}

/// Pre-flight for a closed child spawn: fail with the fix spelled out
/// instead of an undiagnosable OS error. Validates the FINAL staged
/// map with no caller-specific flags: the seed has already populated
/// it, so `PATH` is required for bare program names (and always when
/// `require_path` is set), and `SystemRoot` is required on Windows
/// always. This stays a fail-safe against spawn sites that forget
/// the seed.
pub fn check_spawn_anchors(
    program: &str,
    envs: &HashMap<String, String>,
    require_path: bool,
) -> DiagnosticResult<()> {
    if (require_path || needs_path_lookup(program)) && !has_var(envs, oxdock_env::os::PATH) {
        return Err(proc_diag::spawn_requires_path(program));
    }
    #[allow(clippy::disallowed_macros)]
    if cfg!(windows) && !has_var(envs, oxdock_env::os::windows::SYSTEM_ROOT) {
        return Err(proc_diag::spawn_requires_system_root());
    }
    Ok(())
}

/// OS comparison for variable names: case-insensitive on Windows,
/// exact everywhere else. Mirrors [`has_var`].
pub fn names_eq(a: &str, b: &str) -> bool {
    #[allow(clippy::disallowed_macros)]
    if cfg!(windows) {
        a.eq_ignore_ascii_case(b)
    } else {
        a == b
    }
}

/// One host variable read. Under Miri isolation there is no host to
/// read: every name resolves absent and synthetic fallbacks apply.
#[allow(clippy::disallowed_methods)]
fn host_var(name: &str) -> Option<String> {
    #[allow(clippy::disallowed_macros)]
    if cfg!(miri) {
        None
    } else {
        std::env::var(name).ok()
    }
}

/// Opening chapter of the contract reference, sourced here with the
/// rest of the prose.
pub const CONTRACT_INTRO_MD: &str = indoc::indoc! {"
    ## Seeded environment

    Session start copies host values for the declared names directly
    into the script environment map. A host lacking a name leaves it
    absent: nothing is synthesized, nothing is immutable. Explicit
    script `ENV`/`INHERIT_ENV` overwrite entries uniformly; `ISOLATED`
    resets the map to the same seed. A missing value surfaces at spawn
    time, with the exact `INHERIT_ENV` declaration to add.
"};

/// Baseline-defaults chapter preamble, sourced here with the table.
pub const BASELINE_INTRO_MD: &str = indoc::indoc! {"
    ### Host-mirrored defaults

    Declared names mirrored from the host at session start and on every
    `ISOLATED` entry, wherever the host defines them. Absent stays
    absent; explicit script values win.
"};

/// Explicit-routing chapter, sourced here so the rule lives with the
/// names it constrains.
pub const ROUTING_MD: &str = indoc::indoc! {"
    ### Explicit routing

    The defaults in the table above mirror the host. Everything else
    enters scripts only when listed in `INHERIT_ENV` (inside `ISOLATED`,
    only when listed in the block allowlist, which copies from the
    enclosing scope). Absent stays absent.
"};

/// Precedence and enforcement chapter, sourced here so the guarantee
/// the doc states is reviewed alongside the declaration that keeps it.
pub const PRECEDENCE_MD: &str = indoc::indoc! {"
    ### Precedence and enforcement

    Uniform script-wins: the seed populates mirrors, and explicit
    script `ENV`/`INHERIT_ENV` overwrite them like any other variable.
    No name is immutable. `ISOLATED` resets to the identical seed, so
    inner and outer scopes start from the same defaults.
    `check_spawn_anchors` validates the final staged map with no
    caller-specific flags: present anchors pass, absent ones bail with
    the exact `INHERIT_ENV` declaration to add.

    Source of truth: the `define_env_contract!` invocation in
    `crates/sys/oxdock-process/src/env_policy.rs` (names live in
    `crates/sys/oxdock-fs/src/env.rs`). This file is generated; do not
    edit by hand.
"};

/// Host-reads chapter preamble, sourced here with the inventory it
/// introduces.
pub const HOST_READS_INTRO_MD: &str = indoc::indoc! {"
    ### Host reads

    Variables the engine process itself touches.
"};

/// Engine-process reads are declared in the `define_env_contract!`
/// invocation below.
/// One declared seed row: name plus the platforms seeding it, rendered
/// into the contract table. Tests derive expectations from these rows
/// instead of hardcoding names, so adding a seed row needs no test edits.
pub struct BaselineDefaultEntry {
    /// Variable name (an `oxdock_fs::env` constant value).
    pub name: &'static str,
    /// Platforms seeding the name, rendered verbatim (`unix`,
    /// `windows`, or `unix, windows`).
    pub platforms: &'static str,
}

/// One host interaction: a variable the engine process itself reads
/// (not injects). Names are `env` constants where one exists;
/// `*-suffix` entries are prefix patterns, not single variables. The
/// reader names the code performing the read; there is no prose to rot.
pub struct HostRead {
    /// Variable name, or `PREFIX_*` pattern.
    pub name: &'static str,
    /// Engine function performing the read.
    pub reader: &'static str,
}

/// Declare the whole host contract in one place: seeded names with
/// their owning families, plus engine-process reads. The invocation
/// below expands to the declared name list, the contract entries, and
/// the host reads, so behavior, docs, and tests share one declaration.
/// The interpreter mirrors every declared name the host defines, on
/// every platform: the family lists document ownership for the contract
/// table, they never gate mirroring. Scope shapes are exactly `[unix]`,
/// `[windows]`, `[unix, windows]`; anything else is a compile error at
/// the declaration site.
macro_rules! define_env_contract {
    (
        seeds { $($name:expr, [$($platform:ident),*];)* }
        reads { $($rname:expr, $rreader:expr;)* }
    ) => {
        /// Every declared seed name, on every platform: the seeder
        /// mirrors the ones the host defines (git-bash on Windows
        /// exports `HOME`/`TERM` itself, so unix names resolve there
        /// with no bridge and no detection).
        fn platform_seed_names() -> Vec<&'static str> {
            vec![ $($name),* ]
        }
        /// Every declared seed row on every platform, for the contract
        /// table.
        pub fn describe_baseline_defaults() -> Vec<BaselineDefaultEntry> {
            vec![ $( define_env_contract!(@entry $name, $($platform),*), )* ]
        }
        /// Seed entries for the current platform: identical to the
        /// declaration, since mirroring is platform-blind. What the
        /// child env actually contains, for tests that assert exact dump
        /// key sets without hardcoding names.
        pub fn current_platform_seed() -> Vec<BaselineDefaultEntry> {
            describe_baseline_defaults()
        }
        /// Every host read in the execution path, declared here so the
        /// contract documents interaction as well as seeding.
        pub fn engine_host_reads() -> Vec<HostRead> {
            vec![ $( HostRead { name: $rname, reader: $rreader } ),* ]
        }
    };
    (@entry $name:expr, unix, windows) => {
        BaselineDefaultEntry { name: $name, platforms: "unix, windows" }
    };
    (@entry $name:expr, unix) => {
        BaselineDefaultEntry { name: $name, platforms: "unix" }
    };
    (@entry $name:expr, windows) => {
        BaselineDefaultEntry { name: $name, platforms: "windows" }
    };
}

// The whole host contract: seeded names with their owning families,
// plus engine-process reads. Everything derives from this invocation:
// the declared name list, the contract table, the host-reads table.
// Family lists document ownership; mirroring itself is platform-blind
// (a declared name seeds wherever the host defines it).
define_env_contract! {
    seeds {
        oxdock_env::os::PATH, [unix, windows];
        oxdock_env::os::unix::HOME, [unix];
        oxdock_env::os::unix::TMPDIR, [unix];
        oxdock_env::os::unix::TERM, [unix];
        oxdock_env::os::unix::LANG, [unix];
        oxdock_env::os::unix::LC_ALL, [unix];
        oxdock_env::os::unix::LOGNAME, [unix];
        oxdock_env::os::unix::USER, [unix];
        oxdock_env::os::windows::TEMP, [windows];
        oxdock_env::os::windows::TMP, [windows];
        oxdock_env::os::windows::USERPROFILE, [windows];
        oxdock_env::os::windows::LOCALAPPDATA, [windows];
        oxdock_env::os::windows::APPDATA, [windows];
        oxdock_env::os::windows::SYSTEM_ROOT, [windows];
        oxdock_env::os::windows::WINDIR, [windows];
        oxdock_env::os::windows::PATHEXT, [windows];
        oxdock_env::os::windows::PROGRAM_FILES, [windows];
        oxdock_env::os::windows::PROGRAM_FILES_X86, [windows];
        oxdock_env::os::windows::PROGRAM_W6432, [windows];
        oxdock_env::os::windows::COMSPEC, [windows];
        oxdock_env::os::windows::SYSTEM_DRIVE, [windows];
        oxdock_env::os::windows::PROCESSOR_ARCHITECTURE, [windows];
    }
    reads {
        oxdock_env::os::unix::SHELL, "oxdock_process::shell::shell_program";
        oxdock_env::os::windows::COMSPEC, "oxdock_process::shell::shell_program";
        oxdock_env::os::unix::TMPDIR, "LazyGuardedTempDir::new (via std::env::temp_dir)";
        "CARGO_FEATURE_*", "BuiltinEnv::collect";
        "CARGO_CFG_*", "BuiltinEnv::collect";
    }
}

/// Session seed, written directly into the script env map at
/// session start and reused verbatim for every `ISOLATED` reset: the
/// current platform's declared names mirrored from the host, nothing
/// else. A host lacking a name leaves it absent: no fallbacks, no
/// synthesis. Explicit script `ENV`/`INHERIT_ENV` overwrite these like
/// any other variable. Narrow by name, never wholesale: only the
/// declared rows read the host, so ambient secrets and harness
/// internals still stay out unless explicitly routed.
/// Every name the session seed may inject into the initial script
/// env (the contract baseline table plus the engine-owned rows), for
/// static env-declaration checks: uses of these need no in-script
/// `ENV`/`INHERIT_ENV`, exactly as `ENV_CONTRACTS.md` documents
/// ("arrives mirrored from the host, without declaration"). Mirrors
/// [`seed_defaults`] plus [`BuiltinEnv`](crate::builtin_env::BuiltinEnv)
/// without reading the host, so the checker and the seeder cannot
/// disagree on the set. `CARGO_FEATURE_*`/`CARGO_CFG_*` patterns are
/// not listed; check those with [`is_sanctioned_seed`]. Harness and
/// CLI injected names (`io` overrides) are deliberately absent: they
/// route explicitly through `INHERIT_ENV`, never ambiently.
pub fn seeded_env_names() -> Vec<&'static str> {
    let mut names: Vec<&'static str> = describe_baseline_defaults()
        .iter()
        .map(|entry| entry.name)
        .collect();
    names.push(crate::builtin_env::BuiltinEnv::WORKSPACE_GIT_COMMIT);
    names
}

pub fn seed_defaults() -> HashMap<String, String> {
    interpret_seed_table(&platform_seed_names(), &host_var)
}

/// Interpret one name list into a map. `host` supplies host values so
/// tests can stub the host hermetically; production passes
/// [`host_var`]. A name the host lacks is omitted, never fatal. No
/// variable enters the map undefined.
fn interpret_seed_table(
    names: &[&str],
    host: &dyn Fn(&str) -> Option<String>,
) -> HashMap<String, String> {
    let mut seed = HashMap::with_capacity(names.len());
    for name in names {
        if let Some(mirrored) = host(name) {
            seed.insert((*name).to_string(), mirrored);
        }
    }
    seed
}

/// Transport seed for infrastructure spawns (`ssh`, local binaries):
/// the host `PATH`, loader invariants, terminal default, and profile
/// locations, layered under the sanitized request envs. Mirror or omit,
/// like the session seed: no scratch and no synthesis. An explicitly
/// routed script value always wins.
pub fn seed_transport() -> HashMap<String, String> {
    transport_seed(&host_var)
}

/// Transport seed with a stubbable host lookup (see
/// [`interpret_seed_table`]): production passes [`host_var`].
fn transport_seed(host: &dyn Fn(&str) -> Option<String>) -> HashMap<String, String> {
    let mut seed = HashMap::new();
    for name in transport_names() {
        if let Some(value) = host(name) {
            seed.insert(name.to_string(), value);
        }
    }
    seed
}

/// Host names infrastructure spawns mirror. A subset of the declared
/// seed rows: what it takes to resolve and launch a transport, nothing
/// identifying.
fn transport_names() -> Vec<&'static str> {
    use oxdock_env::os::{unix, windows};
    #[allow(clippy::disallowed_macros)]
    if cfg!(windows) {
        vec![
            oxdock_env::os::PATH,
            windows::SYSTEM_ROOT,
            windows::WINDIR,
            windows::PATHEXT,
            unix::HOME,
            windows::USERPROFILE,
            windows::LOCALAPPDATA,
            windows::APPDATA,
        ]
    } else {
        vec![oxdock_env::os::PATH, unix::TERM, unix::HOME]
    }
}

/// Whether a request entry survives transport sanitize: true when the
/// session seed lacks the name or holds a different value. Key
/// comparison uses [`names_eq`], so Windows casing differences cannot
/// bypass the filter. Explicitly routed values that differ survive.
pub fn seed_differs(seed: &HashMap<String, String>, key: &str, value: &str) -> bool {
    match seed.iter().find(|(k, _)| names_eq(k, key)) {
        Some((_, seeded)) => seeded != value,
        None => true,
    }
}

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

    #[test]
    fn pathed_programs_skip_path_lookup() {
        assert!(!needs_path_lookup("/bin/prog"));
        assert!(!needs_path_lookup("sub/dir/prog"));
        assert!(!needs_path_lookup("sub\\dir\\prog.exe"));
        assert!(needs_path_lookup("prog"));
        assert!(needs_path_lookup("prog.exe"));
    }

    #[test]
    fn sanctioned_seeds_are_cargo_namespaced_only() {
        assert!(is_sanctioned_seed("CARGO_FEATURE_FOO"));
        assert!(is_sanctioned_seed("CARGO_CFG_FOO"));
        assert!(!is_sanctioned_seed(oxdock_env::os::PATH));
        assert!(!is_sanctioned_seed("GITHUB_TOKEN"));
        assert!(!is_sanctioned_seed(oxdock_env::oxdock::WORKSPACE_ROOT));
    }

    #[test]
    fn anchors_reject_bare_spawn_without_path() {
        let envs = HashMap::new();
        let err = check_spawn_anchors("prog", &envs, false).expect_err("must bail");
        assert!(
            err.to_string().contains("INHERIT_ENV [PATH]"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn anchors_require_path_for_shell_form() {
        // Shell binaries resolve by absolute path, but script text inside
        // still resolves through the child's PATH: always required.
        let envs = HashMap::new();
        let err = check_spawn_anchors("/bin/sh", &envs, true).expect_err("must bail");
        assert!(
            err.to_string().contains("INHERIT_ENV [PATH]"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn anchors_accept_pathed_spawn_without_path() {
        let envs = HashMap::new();
        // Absolute program: no PATH consulted. (On Windows this still
        // requires SystemRoot; the assertion below is unix-only.)
        #[allow(clippy::disallowed_macros)]
        if cfg!(windows) {
            let err = check_spawn_anchors("/bin/prog", &envs, false).expect_err("must bail");
            assert!(
                err.to_string().contains("SystemRoot"),
                "unexpected error: {err}"
            );
        } else {
            check_spawn_anchors("/bin/prog", &envs, false).expect("pathed spawn needs no PATH");
        }
    }

    /// Stub host for hermetic proofs: parallel tests cannot skew a
    /// private map, and no test mutates the real process env (a live
    /// `PATH`/`HOME` guard would race concurrent spawns).
    fn stub_host(pairs: &[(&str, &str)]) -> HashMap<String, String> {
        pairs
            .iter()
            .map(|(key, value)| (key.to_string(), value.to_string()))
            .collect()
    }

    #[test]
    fn seed_mirrors_host_essentials() {
        // Every declared name mirrors the stub host, no exceptions.
        // Derived from the declaration, so adding a row needs no
        // edit here.
        let stub: HashMap<String, String> = current_platform_seed()
            .iter()
            .map(|entry| (entry.name.to_string(), format!("host-{}", entry.name)))
            .collect();
        let lookup = |name: &str| stub.get(name).cloned();
        let seed = interpret_seed_table(&platform_seed_names(), &lookup);
        for entry in current_platform_seed() {
            assert_eq!(
                seed.get(entry.name).map(String::as_str),
                stub.get(entry.name).map(String::as_str),
                "{} must mirror the host",
                entry.name
            );
        }
    }

    #[test]
    fn seed_omits_all_without_host() {
        // An empty stub host (Miri, bare `env -i`): every row stays
        // absent. No synthesis, no scratch fallback.
        let seed = interpret_seed_table(&platform_seed_names(), &|_: &str| None::<String>);
        assert!(seed.is_empty(), "empty host must seed nothing: {seed:?}");
    }

    #[test]
    fn seed_admits_only_declared_names() {
        // Closedness: a stub host carrying an unlisted name seeds no
        // trace of it, and every unlisted declared name stays absent.
        let stub = stub_host(&[
            (oxdock_env::os::PATH, "/host/bin"),
            ("HOST_HOME_PROBE", "/nowhere"),
        ]);
        let lookup = |name: &str| stub.get(name).cloned();
        let seed = interpret_seed_table(&platform_seed_names(), &lookup);
        assert!(
            !seed.contains_key("HOST_HOME_PROBE"),
            "unlisted names never seed: {seed:?}"
        );
        assert_eq!(
            seed.get(oxdock_env::os::PATH).map(String::as_str),
            Some("/host/bin")
        );
        for entry in current_platform_seed() {
            if entry.name == oxdock_env::os::PATH {
                continue;
            }
            assert!(
                !seed.contains_key(entry.name),
                "{} must stay absent when the host lacks it: {seed:?}",
                entry.name
            );
        }
    }

    #[test]
    fn seed_differs_spots_seed_equality() {
        // Hermetic: the live process env is not an oracle here (under
        // Miri there is no host to read at all).
        let stub = stub_host(&[(oxdock_env::os::PATH, "/host/bin")]);
        let lookup = |name: &str| stub.get(name).cloned();
        let seed = interpret_seed_table(&platform_seed_names(), &lookup);
        assert!(!seed_differs(&seed, oxdock_env::os::PATH, "/host/bin"));
        assert!(seed_differs(&seed, oxdock_env::os::PATH, "/custom/bin"));
        assert!(seed_differs(&seed, "OXDOCK_CUSTOM_UNSEEDED", "1"));
    }

    #[test]
    fn transport_seed_mirrors_or_omits() {
        // Transports mirror the host names they need; an empty host
        // stages nothing, never a fallback.
        let stub = stub_host(&[(oxdock_env::os::PATH, "/host/bin")]);
        let staged = transport_seed(&|name: &str| stub.get(name).cloned());
        assert_eq!(
            staged.get(oxdock_env::os::PATH).map(String::as_str),
            Some("/host/bin")
        );
        let bare = transport_seed(&|_: &str| None::<String>);
        assert!(bare.is_empty(), "empty host must stage nothing: {bare:?}");
    }

    #[test]
    fn transport_seed_holds_no_target_dir() {
        // Transports mirror only the host names they need, and the
        // target dir is not one of them: routing `cargo` on a remote
        // is the script's explicit job, never ambient.
        let stub: HashMap<String, String> = transport_names()
            .iter()
            .map(|name| (name.to_string(), format!("host-{name}")))
            .collect();
        let lookup = |name: &str| stub.get(name).cloned();
        let staged = transport_seed(&lookup);
        assert!(
            !staged.contains_key(oxdock_env::cargo::TARGET_DIR),
            "transports must not carry a target dir: {staged:?}"
        );
        for name in transport_names() {
            assert_eq!(
                staged.get(name).map(String::as_str),
                stub.get(name).map(String::as_str),
                "{name} must mirror the host"
            );
        }
    }

    #[test]
    fn engine_host_reads_names_are_consts_or_patterns() {
        // Names must be env constants (compile-pinned) or explicit
        // `PREFIX_*` patterns; freeform strings would drift silently.
        let reads = engine_host_reads();
        assert!(!reads.is_empty());
        for read in &reads {
            let known = [
                oxdock_env::os::unix::SHELL,
                oxdock_env::os::windows::COMSPEC,
                oxdock_env::os::unix::TMPDIR,
            ];
            assert!(
                known.contains(&read.name) || read.name.ends_with("_*"),
                "host read {} is neither const nor pattern",
                read.name
            );
            assert!(
                !read.reader.is_empty(),
                "host read {} names no reader",
                read.name
            );
        }
    }

    #[test]
    fn seed_tables_are_consistent() {
        // The declaration pins everything: names unique, every name a
        // defined `env` constant, platforms from the closed set, the
        // interpreter seeding exactly the declared names against a
        // complete stub host, and both entry views agreeing with the
        // name lists.
        let described = describe_baseline_defaults();
        let mut seen = std::collections::HashSet::new();
        for entry in &described {
            assert!(
                seen.insert(entry.name),
                "declaration duplicates {}",
                entry.name
            );
            assert!(
                ["unix", "windows", "unix, windows"].contains(&entry.platforms),
                "{} declares platforms outside the closed set: {}",
                entry.name,
                entry.platforms
            );
        }
        let defined: std::collections::HashSet<&str> = oxdock_env::os::unix::ALL
            .iter()
            .chain(oxdock_env::os::windows::ALL.iter())
            .chain(oxdock_env::cargo::ALL.iter())
            .copied()
            .chain([oxdock_env::os::PATH])
            .collect();
        for entry in &described {
            assert!(
                defined.contains(entry.name),
                "declared {} is not a defined env constant",
                entry.name
            );
        }
        // A complete stub host resolves every declared name; the stub
        // (not the live process env) keeps the proof hermetic on every
        // machine, including Miri.
        let stub: HashMap<String, String> = described
            .iter()
            .map(|entry| (entry.name.to_string(), format!("host-{}", entry.name)))
            .collect();
        let lookup = |name: &str| stub.get(name).cloned();
        let seed = interpret_seed_table(&platform_seed_names(), &lookup);
        for name in platform_seed_names() {
            assert!(seed.contains_key(name), "declared {name} never seeds");
        }
        // Both entry views agree with the name lists they render.
        let mut current: Vec<&str> = current_platform_seed()
            .iter()
            .map(|entry| entry.name)
            .collect();
        let mut names = platform_seed_names();
        current.sort_unstable();
        names.sort_unstable();
        assert_eq!(
            current, names,
            "current-platform entries disagree with seed names"
        );
        for name in platform_seed_names() {
            assert!(
                described.iter().any(|entry| entry.name == name),
                "platform name {name} missing from the contract table"
            );
        }
    }
}