Skip to content

Commit 10ae668

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
fix(desktop): pass augmented PATH to codex-acp version probe (#1794)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
1 parent 6037615 commit 10ae668

3 files changed

Lines changed: 72 additions & 23 deletions

File tree

‎desktop/src-tauri/src/managed_agents/discovery.rs‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -983,38 +983,39 @@ pub(crate) fn classify_runtime(
983983
/// [`std::process::Child::try_wait`] (the repo's standard deadline pattern) and
984984
/// killed if it does not exit in time.
985985
///
986-
/// Stdout is redirected to a temporary file rather than a pipe. A pipe's
987-
/// `read_to_end` blocks until all file descriptors holding the write-end are
988-
/// closed — including those inherited by forked descendants. A regular file does
989-
/// not have this property: `read` returns EOF at the current write position
990-
/// regardless of how many processes still have the file open. This makes the
991-
/// post-exit read guaranteed to complete without blocking, and works identically
992-
/// on Windows and Unix.
986+
/// Stdout is redirected to a temporary file rather than a pipe, so forked
987+
/// descendants cannot hold EOF open. Reads from a regular file return EOF at its
988+
/// current write position regardless of inherited file descriptors, cross-platform.
993989
pub(crate) fn probe_codex_acp_major_version(binary_path: &Path) -> Option<u64> {
990+
probe_codex_acp_major_version_with_path(
991+
binary_path,
992+
crate::managed_agents::readiness::cli_probe::augmented_path().as_deref(),
993+
)
994+
}
995+
pub(crate) fn probe_codex_acp_major_version_with_path(
996+
binary_path: &Path,
997+
augmented_path: Option<&str>,
998+
) -> Option<u64> {
994999
use std::io::{Read as _, Seek as _, SeekFrom};
9951000
use std::time::{Duration, Instant};
996-
997-
/// Hard ceiling on how long the version probe may block discovery.
9981001
const VERSION_PROBE_TIMEOUT: Duration = Duration::from_secs(5);
9991002

1000-
// Redirect stdout to a temporary file instead of a pipe. When the child
1001-
// exits its write-end is closed; any forked descendant that inherited the
1002-
// file descriptor can keep writing, but `read` on a regular file returns
1003-
// EOF at the current file size — not blocking on a "writer present" check.
1004-
// This is the only cross-platform way to bound the post-exit stdout read
1005-
// without O_NONBLOCK (which is Unix-only) or a reader thread.
1003+
// A regular file returns EOF at its current size even when a descendant
1004+
// inherits its descriptor, bounding the post-exit read cross-platform.
10061005
let mut tmp = tempfile::tempfile().ok()?;
10071006

1008-
let mut child = Command::new(binary_path)
1009-
.arg("--version")
1007+
let mut command = Command::new(binary_path);
1008+
command.arg("--version");
1009+
if let Some(path) = augmented_path {
1010+
command.env("PATH", path);
1011+
}
1012+
let mut child = command
10101013
.stdout(tmp.try_clone().ok()?)
10111014
.stderr(std::process::Stdio::null())
10121015
.spawn()
10131016
.ok()?;
10141017

1015-
// Poll try_wait with a deadline — the repo's standard bounded-subprocess
1016-
// pattern (see backend.rs). This returns as soon as the process exits
1017-
// rather than blocking on stdout EOF.
1018+
// Poll until the deadline rather than blocking on stdout EOF.
10181019
let deadline = Instant::now() + VERSION_PROBE_TIMEOUT;
10191020
let exit_status = loop {
10201021
match child.try_wait() {
@@ -1039,9 +1040,7 @@ pub(crate) fn probe_codex_acp_major_version(binary_path: &Path) -> Option<u64> {
10391040
return None;
10401041
}
10411042

1042-
// Seek to the start and read at most 4 KiB. A regular file returns EOF
1043-
// at the current write position even if a descendant still has the fd
1044-
// open, so this read is guaranteed to complete without blocking.
1043+
// Read at most 4 KiB from the regular file without blocking.
10451044
tmp.seek(SeekFrom::Start(0)).ok()?;
10461045
let mut buf = Vec::with_capacity(128);
10471046
let _ = (&mut tmp as &mut dyn std::io::Read)

‎desktop/src-tauri/src/managed_agents/discovery/tests.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,8 @@ fn probe_codex_acp_major_version_parses_1x_output() {
630630
assert_eq!(major, Some(1), "1.x adapter must return major version 1");
631631
}
632632

633+
mod codex_version;
634+
633635
#[cfg(unix)]
634636
#[test]
635637
fn probe_codex_acp_major_version_returns_none_for_nonzero_exit() {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use super::super::probe_codex_acp_major_version_with_path;
2+
3+
#[cfg(unix)]
4+
#[test]
5+
fn probe_codex_acp_major_version_uses_augmented_path_for_env_shebang_interpreter() {
6+
use std::fs;
7+
use std::os::unix::fs::PermissionsExt;
8+
let temp = tempfile::tempdir().expect("temp dir");
9+
let script_dir = temp.path().join("script-bin");
10+
let interpreter_dir = temp.path().join("interpreter-bin");
11+
let empty_path_dir = temp.path().join("empty-bin");
12+
fs::create_dir_all(&script_dir).expect("script dir");
13+
fs::create_dir_all(&interpreter_dir).expect("interpreter dir");
14+
fs::create_dir_all(&empty_path_dir).expect("empty path dir");
15+
16+
let interpreter_path = interpreter_dir.join("node");
17+
fs::write(
18+
&interpreter_path,
19+
"#!/bin/sh\necho '@agentclientprotocol/codex-acp 1.1.2'\n",
20+
)
21+
.expect("write interpreter");
22+
fs::set_permissions(&interpreter_path, fs::Permissions::from_mode(0o755))
23+
.expect("chmod interpreter");
24+
25+
let shim_path = script_dir.join("codex-acp");
26+
fs::write(&shim_path, "#!/usr/bin/env node\n").expect("write shim");
27+
fs::set_permissions(&shim_path, fs::Permissions::from_mode(0o755)).expect("chmod shim");
28+
29+
let scrubbed_path = std::env::join_paths([empty_path_dir.as_path()])
30+
.expect("join scrubbed PATH")
31+
.to_string_lossy()
32+
.into_owned();
33+
assert_eq!(
34+
probe_codex_acp_major_version_with_path(&shim_path, Some(&scrubbed_path)),
35+
None,
36+
"with a scrubbed PATH, /usr/bin/env should not find node"
37+
);
38+
39+
let augmented_path = std::env::join_paths([interpreter_dir.as_path()])
40+
.expect("join augmented PATH")
41+
.to_string_lossy()
42+
.into_owned();
43+
assert_eq!(
44+
probe_codex_acp_major_version_with_path(&shim_path, Some(&augmented_path)),
45+
Some(1),
46+
"the injected augmented PATH should allow /usr/bin/env to find node"
47+
);
48+
}

0 commit comments

Comments
 (0)