Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions codex-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions codex-rs/utils/pty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ pub type SpawnedPty = SpawnedProcess;
pub use pty::conpty_supported;
/// Spawn a process attached to a PTY for interactive use.
pub use pty::spawn_process as spawn_pty_process;
#[cfg(windows)]
pub use win::conpty::RawConPty;
72 changes: 59 additions & 13 deletions codex-rs/utils/pty/src/win/conpty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,78 @@ use portable_pty::PtyPair;
use portable_pty::PtySize;
use portable_pty::PtySystem;
use portable_pty::SlavePty;
use std::mem::ManuallyDrop;
use std::os::windows::io::AsRawHandle;
use std::os::windows::io::RawHandle;
use std::sync::Arc;
use std::sync::Mutex;
use winapi::um::wincon::COORD;

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

fn create_conpty_handles(
size: PtySize,
) -> anyhow::Result<(PsuedoCon, FileDescriptor, FileDescriptor)> {
let stdin = Pipe::new()?;
let stdout = Pipe::new()?;

let con = PsuedoCon::new(
COORD {
X: size.cols as i16,
Y: size.rows as i16,
},
stdin.read,
stdout.write,
)?;

Ok((con, stdin.write, stdout.read))
}

pub struct RawConPty {
con: PsuedoCon,
input_write: FileDescriptor,
output_read: FileDescriptor,
}

impl RawConPty {
pub fn new(cols: i16, rows: i16) -> anyhow::Result<Self> {
let (con, input_write, output_read) = create_conpty_handles(PtySize {
rows: rows as u16,
cols: cols as u16,
pixel_width: 0,
pixel_height: 0,
})?;
Ok(Self {
con,
input_write,
output_read,
})
}

pub fn pseudoconsole_handle(&self) -> RawHandle {
self.con.raw_handle()
}

pub fn into_raw_handles(self) -> (RawHandle, RawHandle, RawHandle) {
let me = ManuallyDrop::new(self);
(
me.con.raw_handle(),
me.input_write.as_raw_handle(),
me.output_read.as_raw_handle(),
)
}
}

impl PtySystem for ConPtySystem {
fn openpty(&self, size: PtySize) -> anyhow::Result<PtyPair> {
let stdin = Pipe::new()?;
let stdout = Pipe::new()?;

let con = PsuedoCon::new(
COORD {
X: size.cols as i16,
Y: size.rows as i16,
},
stdin.read,
stdout.write,
)?;
let (con, writable, readable) = create_conpty_handles(size)?;

let master = ConPtyMasterPty {
inner: Arc::new(Mutex::new(Inner {
con,
readable: stdout.read,
writable: Some(stdin.write),
readable,
writable: Some(writable),
size,
})),
};
Expand Down
4 changes: 4 additions & 0 deletions codex-rs/utils/pty/src/win/psuedocon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ impl Drop for PsuedoCon {
}

impl PsuedoCon {
pub fn raw_handle(&self) -> HPCON {
self.con
}

pub fn new(size: COORD, input: FileDescriptor, output: FileDescriptor) -> Result<Self, Error> {
let mut con: HPCON = INVALID_HANDLE_VALUE;
let result = unsafe {
Expand Down
5 changes: 5 additions & 0 deletions codex-rs/windows-sandbox-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ chrono = { version = "0.4.42", default-features = false, features = [
"clock",
"std",
] }
codex-utils-pty = { workspace = true }
codex-utils-absolute-path = { workspace = true }
codex-utils-string = { workspace = true }
dunce = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tempfile = "3"
tokio = { workspace = true, features = ["sync", "rt"] }
windows = { version = "0.58", features = [
"Win32_Foundation",
"Win32_NetworkManagement_WindowsFirewall",
Expand Down Expand Up @@ -86,3 +88,6 @@ pretty_assertions = { workspace = true }

[build-dependencies]
winres = "0.1"

[package.metadata.cargo-shear]
ignored = ["codex-utils-pty", "tokio"]
2 changes: 1 addition & 1 deletion codex-rs/windows-sandbox-rs/src/bin/command_runner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[path = "../command_runner_win.rs"]
#[path = "../elevated/command_runner_win.rs"]
mod win;

#[cfg(target_os = "windows")]
Expand Down
Loading
Loading