mirror of
https://github.com/openai/codex.git
synced 2026-11-23 23:10:47 +00:00
## Why Sandbox helpers with non-UTF-8 paths can lose their exact executable path when converted into command requests. PTY launches with a custom `argv[0]` can also try to execute the dispatch name instead of the configured program. ## What changed - Carry native executable paths through sandbox requests, local execution, shell snapshots, and sandbox integrity checks. - Use native Unix PTY spawning when `argv[0]` differs from the executable, preserving the dispatch name and controlling terminal setup. - Preserve PTY executable lookup through the child environment's `PATH` and fallback to its home directory when the requested working directory is missing. ## Testing Add regression coverage for non-UTF-8 sandbox helper paths, PTY dispatch names, child `HOME`/`PATH` lookup, controlling terminals, and exit-code preservation. GitOrigin-RevId: 6a2af0486220d98195f5d67fde2597e3c4515e11
105 lines
2.9 KiB
Rust
105 lines
2.9 KiB
Rust
#[cfg(unix)]
|
|
use std::path::Path;
|
|
use std::time::Duration;
|
|
|
|
use codex_utils_pty::ProcessDriver;
|
|
use codex_utils_pty::SpawnedProcess;
|
|
use codex_utils_pty::spawn_from_driver;
|
|
use pretty_assertions::assert_eq;
|
|
use tokio::sync::broadcast;
|
|
use tokio::sync::mpsc;
|
|
use tokio::sync::oneshot;
|
|
use tokio::time::timeout;
|
|
|
|
use super::respond_to_terminal_queries;
|
|
#[cfg(unix)]
|
|
use crate::SandboxType;
|
|
#[cfg(unix)]
|
|
use crate::SpawnRequest;
|
|
#[cfg(unix)]
|
|
use crate::spawn_process;
|
|
|
|
#[tokio::test]
|
|
async fn driver_backed_terminal_queries_are_answered() -> anyhow::Result<()> {
|
|
let (writer_tx, mut writer_rx) = mpsc::channel(8);
|
|
let (stdout_tx, stdout_rx) = broadcast::channel(8);
|
|
let (exit_tx, exit_rx) = oneshot::channel();
|
|
let spawned = respond_to_terminal_queries(spawn_from_driver(ProcessDriver {
|
|
writer_tx,
|
|
stdout_rx,
|
|
stderr_rx: None,
|
|
exit_rx,
|
|
terminator: None,
|
|
writer_handle: None,
|
|
resizer: None,
|
|
#[cfg(windows)]
|
|
tty: true,
|
|
}));
|
|
let SpawnedProcess {
|
|
session: _session,
|
|
mut stdout_rx,
|
|
..
|
|
} = spawned;
|
|
|
|
stdout_tx.send(b"before\x1b[".to_vec())?;
|
|
stdout_tx.send(b"5n\x1b[18t\x1b[6n\x1b[?1049$p\x1b[31mafter".to_vec())?;
|
|
let responses = timeout(Duration::from_secs(2), writer_rx.recv())
|
|
.await?
|
|
.ok_or_else(|| anyhow::anyhow!("terminal response channel closed"))?;
|
|
assert_eq!(responses, b"\x1b[0n\x1b[8;24;80t\x1b[1;1R\x1b[?1049;0$y");
|
|
|
|
drop(stdout_tx);
|
|
exit_tx.send(0).expect("send exit code");
|
|
let output = timeout(Duration::from_secs(2), async move {
|
|
let mut output = Vec::new();
|
|
while let Some(chunk) = stdout_rx.recv().await {
|
|
output.extend(chunk);
|
|
}
|
|
output
|
|
})
|
|
.await?;
|
|
assert_eq!(output, b"before\x1b[31mafter".to_vec());
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[tokio::test]
|
|
async fn direct_terminal_queries_are_answered() -> anyhow::Result<()> {
|
|
let script =
|
|
"stty -echo -icanon; printf 'alpha\\033[6n'; dd bs=1 count=6 2>/dev/null; printf '\\nok'";
|
|
let env = std::env::vars().collect();
|
|
let spawned = spawn_process(SpawnRequest {
|
|
program: "/bin/sh".as_ref(),
|
|
args: &["-c".to_string(), script.to_string()],
|
|
cwd: Path::new("."),
|
|
env: &env,
|
|
arg0: &None,
|
|
sandbox: SandboxType::None,
|
|
windows_sandbox: None,
|
|
tty: true,
|
|
stdin_open: true,
|
|
inherited_fds: codex_utils_pty::ChildFds::Inherited(&[]),
|
|
})
|
|
.await?;
|
|
let SpawnedProcess {
|
|
session: _session,
|
|
mut stdout_rx,
|
|
exit_rx,
|
|
..
|
|
} = spawned;
|
|
let (code, output) = timeout(Duration::from_secs(5), async move {
|
|
let code = exit_rx.await?;
|
|
let mut output = Vec::new();
|
|
while let Some(chunk) = stdout_rx.recv().await {
|
|
output.extend(chunk);
|
|
}
|
|
anyhow::Ok((code, output))
|
|
})
|
|
.await??;
|
|
|
|
assert_eq!((code, output), (0, b"alpha\x1b[1;1R\r\nok".to_vec()));
|
|
|
|
Ok(())
|
|
}
|