Avoid redundant filesystem probes (#36393)

## What changed

- Load `environments.toml` in one operation, falling back to the default
  environment provider only when the read reports that the file is missing.
- Attempt the default daemon socket connection directly instead of checking the
  socket path first.
- Preserve each file search entry's type from the directory walker, avoiding a
  second metadata lookup when producing matches and correctly classifying
  followed directory symlinks.
- Write atomic-file contents through the existing temporary-file handle rather
  than reopening its path.

## Testing

- Add Unix coverage for classifying a followed directory symlink as a
  directory in file search results.

GitOrigin-RevId: aa6739cac23953ca8b9a3fcb53e43e72c41cfdf9
This commit is contained in:
Charlie Marsh
2026-07-31 21:37:56 +00:00
committed by copyberry
parent dc60dadce6
commit 003ec63bba
4 changed files with 74 additions and 40 deletions
+25 -22
View File
@@ -197,16 +197,10 @@ pub(crate) fn environment_provider_from_codex_home(
codex_home: &Path,
) -> Result<Box<dyn EnvironmentProvider>, ExecServerError> {
let path = codex_home.join(ENVIRONMENTS_TOML_FILE);
if !path.try_exists().map_err(|err| {
ExecServerError::Protocol(format!(
"failed to inspect environment config `{}`: {err}",
path.display()
))
})? {
let Some(environments) = load_environments_toml(&path)? else {
return Ok(Box::new(DefaultEnvironmentProvider::from_env()));
}
};
let environments = load_environments_toml(&path)?;
Ok(Box::new(TomlEnvironmentProvider::new_with_config_dir(
environments,
Some(codex_home),
@@ -295,20 +289,27 @@ fn validate_websocket_url(url: String) -> Result<String, ExecServerError> {
Ok(url.to_string())
}
fn load_environments_toml(path: &Path) -> Result<EnvironmentsToml, ExecServerError> {
let contents = std::fs::read_to_string(path).map_err(|err| {
ExecServerError::Protocol(format!(
"failed to read environment config `{}`: {err}",
path.display()
))
})?;
/// Returns `None` when the config is missing; other I/O and parse failures remain errors.
fn load_environments_toml(path: &Path) -> Result<Option<EnvironmentsToml>, ExecServerError> {
let contents = match std::fs::read_to_string(path) {
Ok(contents) => contents,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(err) => {
return Err(ExecServerError::Protocol(format!(
"failed to read environment config `{}`: {err}",
path.display()
)));
}
};
toml::from_str(&contents).map_err(|err| {
ExecServerError::Protocol(format!(
"failed to parse environment config `{}`: {err}",
path.display()
))
})
toml::from_str(&contents)
.map_err(|err| {
ExecServerError::Protocol(format!(
"failed to parse environment config `{}`: {err}",
path.display()
))
})
.map(Some)
}
mod option_duration_secs {
@@ -756,7 +757,9 @@ CODEX_LOG = "debug"
)
.expect("write environments.toml");
let environments = load_environments_toml(&path).expect("environments.toml");
let environments = load_environments_toml(&path)
.expect("environments.toml")
.expect("environments.toml should exist");
assert_eq!(environments.default.as_deref(), Some("ssh-dev"));
assert_eq!(environments.include_local, Some(false));