Skip to content

Commit d0a693e

Browse files
windows-sandbox: add runner IPC foundation for future unified_exec (#14139)
# Summary This PR introduces the Windows sandbox runner IPC foundation that later unified_exec work will build on. The key point is that this is intentionally infrastructure-only. The new IPC transport, runner plumbing, and ConPTY helpers are added here, but the active elevated Windows sandbox path still uses the existing request-file bootstrap. In other words, this change prepares the transport and module layout we need for unified_exec without switching production behavior over yet. Part of this PR is also a source-layout cleanup: some Windows sandbox files are moved into more explicit `elevated/`, `conpty/`, and shared locations so it is clearer which code is for the elevated sandbox flow, which code is legacy/direct-spawn behavior, and which helpers are shared between them. That reorganization is intentional in this first PR so later behavioral changes do not also have to carry a large amount of file-move churn. # Why This Is Needed For unified_exec Windows elevated sandboxed unified_exec needs a long-lived, bidirectional control channel between the CLI and a helper process running under the sandbox user. That channel has to support: - starting a process and reporting structured spawn success/failure - streaming stdout/stderr back incrementally - forwarding stdin over time - terminating or polling a long-lived process - supporting both pipe-backed and PTY-backed sessions The existing elevated one-shot path is built around a request-file bootstrap and does not provide those primitives cleanly. Before we can turn on Windows sandbox unified_exec, we need the underlying runner protocol and transport layer that can carry those lifecycle events and streams. # Why Windows Needs More Machinery Than Linux Or macOS Linux and macOS can generally build unified_exec on top of the existing sandbox/process model: the parent can spawn the child directly, retain normal ownership of stdio or PTY handles, and manage the lifetime of the sandboxed process without introducing a second control process. Windows elevated sandboxing is different. To run inside the sandbox boundary, we cross into a different user/security context and then need to manage a long-lived process from outside that boundary. That means we need an explicit helper process plus an IPC transport to carry spawn, stdin, output, and exit events back and forth. The extra code here is mostly that missing Windows sandbox infrastructure, not a conceptual difference in unified_exec itself. # What This PR Adds - the framed IPC message types and transport helpers for parent <-> runner communication - the renamed Windows command runner with both the existing request-file bootstrap and the dormant IPC bootstrap - named-pipe helpers for the elevated runner path - ConPTY helpers and process-thread attribute plumbing needed for PTY-backed sessions - shared sandbox/process helpers that later PRs will reuse when switching live execution paths over - early file/module moves so later PRs can focus on behavior rather than layout churn # What This PR Does Not Yet Do - it does not switch the active elevated one-shot path over to IPC yet - it does not enable Windows sandbox unified_exec yet - it does not remove the existing request-file bootstrap yet So while this code compiles and the new path has basic validation, it is not yet the exercised production path. That is intentional for this first PR: the goal here is to land the transport and runner foundation cleanly before later PRs start routing real command execution through it. # Follow-Ups Planned follow-up PRs will: 1. switch elevated one-shot Windows sandbox execution to the new runner IPC path 2. layer Windows sandbox unified_exec sessions on top of the same transport 3. remove the legacy request-file path once the IPC-based path is live # Validation - `cargo build -p codex-windows-sandbox`
1 parent 4c9dbc1 commit d0a693e

File tree

18 files changed

+1656
-343
lines changed

18 files changed

+1656
-343
lines changed

codex-rs/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codex-rs/utils/pty/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ pub type SpawnedPty = SpawnedProcess;
2929
pub use pty::conpty_supported;
3030
/// Spawn a process attached to a PTY for interactive use.
3131
pub use pty::spawn_process as spawn_pty_process;
32+
#[cfg(windows)]
33+
pub use win::conpty::RawConPty;

codex-rs/utils/pty/src/win/conpty.rs

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,78 @@ use portable_pty::PtyPair;
2929
use portable_pty::PtySize;
3030
use portable_pty::PtySystem;
3131
use portable_pty::SlavePty;
32+
use std::mem::ManuallyDrop;
33+
use std::os::windows::io::AsRawHandle;
34+
use std::os::windows::io::RawHandle;
3235
use std::sync::Arc;
3336
use std::sync::Mutex;
3437
use winapi::um::wincon::COORD;
3538

3639
#[derive(Default)]
3740
pub struct ConPtySystem {}
3841

42+
fn create_conpty_handles(
43+
size: PtySize,
44+
) -> anyhow::Result<(PsuedoCon, FileDescriptor, FileDescriptor)> {
45+
let stdin = Pipe::new()?;
46+
let stdout = Pipe::new()?;
47+
48+
let con = PsuedoCon::new(
49+
COORD {
50+
X: size.cols as i16,
51+
Y: size.rows as i16,
52+
},
53+
stdin.read,
54+
stdout.write,
55+
)?;
56+
57+
Ok((con, stdin.write, stdout.read))
58+
}
59+
60+
pub struct RawConPty {
61+
con: PsuedoCon,
62+
input_write: FileDescriptor,
63+
output_read: FileDescriptor,
64+
}
65+
66+
impl RawConPty {
67+
pub fn new(cols: i16, rows: i16) -> anyhow::Result<Self> {
68+
let (con, input_write, output_read) = create_conpty_handles(PtySize {
69+
rows: rows as u16,
70+
cols: cols as u16,
71+
pixel_width: 0,
72+
pixel_height: 0,
73+
})?;
74+
Ok(Self {
75+
con,
76+
input_write,
77+
output_read,
78+
})
79+
}
80+
81+
pub fn pseudoconsole_handle(&self) -> RawHandle {
82+
self.con.raw_handle()
83+
}
84+
85+
pub fn into_raw_handles(self) -> (RawHandle, RawHandle, RawHandle) {
86+
let me = ManuallyDrop::new(self);
87+
(
88+
me.con.raw_handle(),
89+
me.input_write.as_raw_handle(),
90+
me.output_read.as_raw_handle(),
91+
)
92+
}
93+
}
94+
3995
impl PtySystem for ConPtySystem {
4096
fn openpty(&self, size: PtySize) -> anyhow::Result<PtyPair> {
41-
let stdin = Pipe::new()?;
42-
let stdout = Pipe::new()?;
43-
44-
let con = PsuedoCon::new(
45-
COORD {
46-
X: size.cols as i16,
47-
Y: size.rows as i16,
48-
},
49-
stdin.read,
50-
stdout.write,
51-
)?;
97+
let (con, writable, readable) = create_conpty_handles(size)?;
5298

5399
let master = ConPtyMasterPty {
54100
inner: Arc::new(Mutex::new(Inner {
55101
con,
56-
readable: stdout.read,
57-
writable: Some(stdin.write),
102+
readable,
103+
writable: Some(writable),
58104
size,
59105
})),
60106
};

codex-rs/utils/pty/src/win/psuedocon.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ impl Drop for PsuedoCon {
130130
}
131131

132132
impl PsuedoCon {
133+
pub fn raw_handle(&self) -> HPCON {
134+
self.con
135+
}
136+
133137
pub fn new(size: COORD, input: FileDescriptor, output: FileDescriptor) -> Result<Self, Error> {
134138
let mut con: HPCON = INVALID_HANDLE_VALUE;
135139
let result = unsafe {

codex-rs/windows-sandbox-rs/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ chrono = { version = "0.4.42", default-features = false, features = [
2424
"clock",
2525
"std",
2626
] }
27+
codex-utils-pty = { workspace = true }
2728
codex-utils-absolute-path = { workspace = true }
2829
codex-utils-string = { workspace = true }
2930
dunce = "1.0"
3031
serde = { version = "1.0", features = ["derive"] }
3132
serde_json = "1.0"
3233
tempfile = "3"
34+
tokio = { workspace = true, features = ["sync", "rt"] }
3335
windows = { version = "0.58", features = [
3436
"Win32_Foundation",
3537
"Win32_NetworkManagement_WindowsFirewall",
@@ -86,3 +88,6 @@ pretty_assertions = { workspace = true }
8688

8789
[build-dependencies]
8890
winres = "0.1"
91+
92+
[package.metadata.cargo-shear]
93+
ignored = ["codex-utils-pty", "tokio"]

codex-rs/windows-sandbox-rs/src/bin/command_runner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[path = "../command_runner_win.rs"]
1+
#[path = "../elevated/command_runner_win.rs"]
22
mod win;
33

44
#[cfg(target_os = "windows")]

0 commit comments

Comments
 (0)