diff --git a/codex-rs/tui/src/clipboard_copy/wsl.rs b/codex-rs/tui/src/clipboard_copy/wsl.rs index d1c89cc9e3..74bcf97c52 100644 --- a/codex-rs/tui/src/clipboard_copy/wsl.rs +++ b/codex-rs/tui/src/clipboard_copy/wsl.rs @@ -1,57 +1,151 @@ //! WSL text copies with Windows interop use the Windows clipboard without contacting the display. +//! Use the lightweight Windows clip writer for single-line ASCII; PowerShell preserves other text. //! Bound discovery, input, and exit; give fallback native setup its own short deadline. //! Never release the worker while a writer can still run. -#[cfg(target_os = "linux")] -use std::ffi::OsString; -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", all(test, unix)))] use std::path::PathBuf; -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", all(test, unix)))] use std::sync::Mutex; -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", all(test, unix)))] use std::sync::mpsc; #[cfg(any(target_os = "linux", all(test, unix)))] use std::time::Instant; -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", all(test, unix)))] type Pending = Mutex>>>; #[cfg(target_os = "linux")] pub(super) fn copy(text: &str, deadline: Instant) -> Result<(), String> { - static DISCOVERY: Pending<(PathBuf, OsString)> = Mutex::new(None); - let (executable, path) = read_only_setup(&DISCOVERY, deadline, || { - let executable = codex_utils_path::system_executable("powershell.exe") - .ok_or("PowerShell is unavailable in the system PATH")?; + static SETUP: Pending<(std::ffi::OsString, Option)> = Mutex::new(None); + static CLIP: Pending> = Mutex::new(None); + let (path, adapter) = read_only_setup(&SETUP, deadline, || { let path = codex_utils_path::system_path() .map_err(|error| format!("failed to resolve system PATH: {error}"))?; - Ok((executable, path)) + // An installed Linux adapter takes precedence over both Windows writers. Avoid + // looking up the built-in PowerShell until clip.exe actually needs a fallback. + let adapter = codex_utils_path::linux_system_executable("powershell.exe"); + Ok((path, adapter)) })?; - let status = tokio::runtime::Builder::new_current_thread() + let clip = (adapter.is_none() && clip_input(text).is_some()) + .then(|| { + read_only_setup( + &CLIP, + deadline.min(Instant::now() + std::time::Duration::from_millis(/*millis*/ 200)), + || Ok(codex_utils_path::system_executable("clip.exe")), + ) + .ok() + .flatten() + }) + .flatten(); + let runtime = tokio::runtime::Builder::new_current_thread() .enable_all() .build() - .map_err(|error| format!("could not start Windows clipboard writer: {error}"))? - .block_on(write_command(command(&executable, &path), text, deadline))?; + .map_err(|error| format!("could not start Windows clipboard writer: {error}"))?; + runtime.block_on(copy_with_writers( + text, + deadline, + &path, + adapter.as_deref(), + clip.as_deref(), + )) +} + +#[cfg(any(target_os = "linux", all(test, unix)))] +async fn copy_with_writers( + text: &str, + deadline: Instant, + path: &std::ffi::OsStr, + adapter: Option<&std::path::Path>, + clip: Option<&std::path::Path>, +) -> Result<(), String> { + static POWERSHELL: Pending> = Mutex::new(None); + if let Some(adapter) = adapter { + return write_writer(command(adapter, path), text, deadline, "powershell.exe").await; + } + let clip_error = if let Some(clip) = clip + && let Some(input) = clip_input(text) + { + match write_writer( + shell_command(clip, path), + &input, + deadline.min(Instant::now() + std::time::Duration::from_secs(/*secs*/ 1)), + "clip.exe", + ) + .await + { + Ok(()) => return Ok(()), + Err(error) => Some(error), + } + } else { + None + }; + let fallback = async { + let powershell = read_only_setup(&POWERSHELL, deadline, || { + Ok(codex_utils_path::system_executable("powershell.exe")) + })? + .ok_or("PowerShell is unavailable in the system PATH")?; + write_writer(command(&powershell, path), text, deadline, "powershell.exe").await + } + .await; + fallback.map_err(|error| match clip_error { + Some(clip_error) => format!("{clip_error}; {error}"), + None => error, + }) +} + +#[cfg(any(target_os = "linux", all(test, unix)))] +async fn write_writer( + command: tokio::process::Command, + text: &str, + deadline: Instant, + name: &str, +) -> Result<(), String> { + let status = write_command(command, text, deadline) + .await + .map_err(|error| format!("{name}: {error}"))?; if status.success() { Ok(()) } else { - Err(format!("powershell.exe exited with status {status}")) + Err(format!("{name} exited with status {status}")) } } +#[cfg(any(target_os = "linux", all(test, unix)))] +fn clip_input(text: &str) -> Option> { + // clip.exe normalizes newlines and guesses UTF-16 for some even-length ASCII. + // An odd byte count forces OEM text; a trailing NUL does not enter the clipboard. + text.bytes() + .all(|byte| byte.is_ascii_graphic() || matches!(byte, b' ' | b'\t')) + .then(|| { + if text.len().is_multiple_of(2) { + std::borrow::Cow::Owned(format!("{text}\0")) + } else { + std::borrow::Cow::Borrowed(text) + } + }) +} + #[cfg(any(target_os = "linux", all(test, unix)))] fn command(executable: &std::path::Path, path: &std::ffi::OsStr) -> tokio::process::Command { + let mut command = shell_command(executable, path); + // Preserve the released PowerShell signature: trusted adapters may match it exactly. + command.args([ + "-NoProfile", + "-Command", + "[Console]::InputEncoding = [System.Text.Encoding]::UTF8; $ErrorActionPreference = 'Stop'; $text = [Console]::In.ReadToEnd(); Set-Clipboard -Value $text", + ]); + command +} + +#[cfg(any(target_os = "linux", all(test, unix)))] +fn shell_command(executable: &std::path::Path, path: &std::ffi::OsStr) -> tokio::process::Command { // Spawn only the native shell here: directly spawning a Windows exe can block on WSL. let mut command = tokio::process::Command::new("/bin/sh"); command .env("PATH", path) .args(["-c", r#"exec "$@""#, "codex-wsl-copy"]) - .arg(executable) - // Preserve the released command signature: trusted adapters may match it exactly. - .args([ - "-NoProfile", - "-Command", - "[Console]::InputEncoding = [System.Text.Encoding]::UTF8; $ErrorActionPreference = 'Stop'; $text = [Console]::In.ReadToEnd(); Set-Clipboard -Value $text", - ]); + .arg(executable); command } @@ -72,7 +166,7 @@ pub(super) fn native_clipboard() -> Result { // Discovery and WSLg setup may block forever. Keep at most one outstanding task of each // kind and reuse its result after a timeout. Neither task may capture text or write a clipboard. -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", all(test, unix)))] fn read_only_setup( pending: &Pending, deadline: Instant, @@ -127,7 +221,7 @@ async fn write_command( .stderr(Stdio::null()) .kill_on_drop(/*kill_on_drop*/ true) .spawn() - .map_err(|error| format!("failed to spawn powershell.exe: {error}"))?; + .map_err(|error| format!("failed to spawn Windows clipboard writer: {error}"))?; // WSL process spawn itself may stall. Do not write after a late spawn. let result = tokio::time::timeout_at(deadline.into(), async { if Instant::now() >= deadline { @@ -136,15 +230,15 @@ async fn write_command( child .stdin .as_mut() - .ok_or("failed to open powershell.exe stdin")? + .ok_or("failed to open Windows clipboard writer stdin")? .write_all(text.as_bytes()) .await - .map_err(|error| format!("failed to write to powershell.exe: {error}"))?; + .map_err(|error| format!("failed to write to Windows clipboard writer: {error}"))?; drop(child.stdin.take()); child .wait() .await - .map_err(|error| format!("failed to wait for powershell.exe: {error}")) + .map_err(|error| format!("failed to wait for Windows clipboard writer: {error}")) }) .await .unwrap_or_else(|_| Err("Windows clipboard writer timed out".into())); @@ -166,7 +260,7 @@ async fn write_command( child .wait() .await - .map_err(|error| format!("failed to reap powershell.exe: {error}"))?; + .map_err(|error| format!("failed to reap Windows clipboard writer: {error}"))?; Err(error) } } diff --git a/codex-rs/tui/src/clipboard_copy/wsl_tests.rs b/codex-rs/tui/src/clipboard_copy/wsl_tests.rs index 895fcc7616..e6078d52b4 100644 --- a/codex-rs/tui/src/clipboard_copy/wsl_tests.rs +++ b/codex-rs/tui/src/clipboard_copy/wsl_tests.rs @@ -1,3 +1,5 @@ +#![cfg(test)] + use super::write_command; use pretty_assertions::assert_eq; use std::time::Duration; @@ -7,6 +9,16 @@ use std::time::Instant; async fn installed_adapter_receives_compatible_command_and_never_copies_expired_work() { use std::os::unix::fs::PermissionsExt; + for (text, input) in [ + ("Clipboard latency", Some("Clipboard latency")), + ("Bush hid the facts", Some("Bush hid the facts\0")), + ("é", None), + ("line\n", None), + ("nul\0", None), + ] { + assert_eq!(super::clip_input(text).as_deref(), input, "{text:?}"); + } + let dir = tempfile::tempdir().unwrap(); let root = dir.path().canonicalize().unwrap(); let executable = root.join("powershell.exe"); @@ -15,13 +27,36 @@ async fn installed_adapter_receives_compatible_command_and_never_copies_expired_ r#"#!/bin/sh [ "$#" -eq 3 ] && [ "$1" = '-NoProfile' ] && [ "$2" = '-Command' ] || exit 46 [ "$3" = "[Console]::InputEncoding = [System.Text.Encoding]::UTF8; \$ErrorActionPreference = 'Stop'; \$text = [Console]::In.ReadToEnd(); Set-Clipboard -Value \$text" ] || exit 47 -/bin/cat > "$COPY_OUTPUT" +/bin/cat > "${0%/*}/clipboard" "#, ).unwrap(); std::fs::set_permissions(&executable, std::fs::Permissions::from_mode(0o755)).unwrap(); let output = root.join("clipboard"); - let mut command = super::command(&executable, root.as_os_str()); - command.env("COPY_OUTPUT", &output); + let command = super::command(&executable, root.as_os_str()); + // With both writers present, ASCII must go through the installed adapter. + super::copy_with_writers( + "one line", + Instant::now() + Duration::from_secs(/*secs*/ 5), + root.as_os_str(), + Some(&executable), + Some(std::path::Path::new("/usr/bin/true")), + ) + .await + .unwrap(); + assert_eq!(std::fs::read_to_string(&output).unwrap(), "one line"); + let error = super::copy_with_writers( + "one line", + Instant::now(), + root.as_os_str(), + /*adapter*/ None, + Some(std::path::Path::new("/usr/bin/true")), + ) + .await + .unwrap_err(); + assert!( + error.contains("clip.exe") && error.contains("clipboard setup timed out"), + "{error}" + ); let text = " café 🦀\r\n\t".repeat(/*n*/ 32_768); let status = write_command( command, @@ -32,8 +67,7 @@ async fn installed_adapter_receives_compatible_command_and_never_copies_expired_ .unwrap(); assert!(status.success(), "{status}"); assert_eq!(std::fs::read_to_string(&output).unwrap(), text); - let mut command = super::command(&executable, root.as_os_str()); - command.env("COPY_OUTPUT", &output); + let command = super::command(&executable, root.as_os_str()); assert_eq!( write_command(command, "late", Instant::now()).await, Err("Windows clipboard writer timed out".into()) diff --git a/codex-rs/utils/path-utils/src/lib.rs b/codex-rs/utils/path-utils/src/lib.rs index 118876d183..b13bc834c5 100644 --- a/codex-rs/utils/path-utils/src/lib.rs +++ b/codex-rs/utils/path-utils/src/lib.rs @@ -3,6 +3,8 @@ pub(crate) mod env; pub use env::is_wsl; mod system_commands; +#[cfg(target_os = "linux")] +pub use system_commands::linux_system_executable; pub use system_commands::system_executable; pub use system_commands::system_path; diff --git a/codex-rs/utils/path-utils/src/system_commands.rs b/codex-rs/utils/path-utils/src/system_commands.rs index 7e51da40aa..8c32fdda4b 100644 --- a/codex-rs/utils/path-utils/src/system_commands.rs +++ b/codex-rs/utils/path-utils/src/system_commands.rs @@ -12,12 +12,18 @@ use std::path::PathBuf; /// Finds an installed helper without consulting PATH, PATHEXT, or the working directory. pub fn system_executable(name: &str) -> Option { - executable_in_directories(name, &system_directories()) + executable_in_directories(name, &system_directories(ExecutableScope::All)) +} + +/// Finds a trusted Linux installation without probing WSL's Windows executable directories. +#[cfg(target_os = "linux")] +pub fn linux_system_executable(name: &str) -> Option { + executable_in_directories(name, &system_directories(ExecutableScope::Linux)) } /// Child-process PATH for automatic helpers, not for user-requested tools. pub fn system_path() -> std::io::Result { - let directories = system_directories(); + let directories = system_directories(ExecutableScope::All); if directories.is_empty() { return Err(std::io::Error::other("no trusted executable directories")); } @@ -72,7 +78,15 @@ fn installation_roots() -> Vec { .collect() } -fn system_directories() -> Vec { +enum ExecutableScope { + All, + #[cfg(target_os = "linux")] + Linux, +} + +fn system_directories(scope: ExecutableScope) -> Vec { + #[cfg(not(target_os = "linux"))] + let _ = scope; #[cfg(unix)] let directories = [ "/opt/homebrew/bin", @@ -91,7 +105,7 @@ fn system_directories() -> Vec { .map(PathBuf::from); #[cfg(target_os = "linux")] let directories = directories.chain( - crate::is_wsl() + (matches!(scope, ExecutableScope::All) && crate::is_wsl()) .then_some([ PathBuf::from("/mnt/c/Windows/System32"), PathBuf::from("/mnt/c/Windows/System32/WindowsPowerShell/v1.0"),