mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Fix config reads with relative working directories (#61663)
Build `config/read` responses from the latest loaded configuration's layer stack so requests with a relative `cwd`, such as `.`, succeed and reflect external configuration edits. Reuse that configuration for feature overrides and preserve precedence for layer read and decoding errors over full-config validation errors. Add a regression test that reads with `cwd: "."` before and after an external model configuration edit and checks that both values are returned. GitOrigin-RevId: 82eb835f2eac4e53414b01fc9286c2aac71babf2
This commit is contained in:
@@ -137,23 +137,28 @@ impl ConfigManager {
|
||||
&self,
|
||||
params: ConfigReadParams,
|
||||
) -> Result<ConfigReadResponse, ConfigManagerError> {
|
||||
let layers = match params.cwd.as_deref() {
|
||||
Some(cwd) => {
|
||||
let cwd = AbsolutePathBuf::try_from(PathBuf::from(cwd)).map_err(|err| {
|
||||
let cwd = params
|
||||
.cwd
|
||||
.map(|cwd| {
|
||||
AbsolutePathBuf::try_from(PathBuf::from(cwd)).map_err(|err| {
|
||||
ConfigManagerError::io("failed to resolve config cwd to an absolute path", err)
|
||||
})?;
|
||||
self.load_config_layers(Some(cwd)).await.map_err(|err| {
|
||||
ConfigManagerError::io("failed to read configuration layers", err)
|
||||
})?
|
||||
}
|
||||
None => self.load_thread_agnostic_config().await.map_err(|err| {
|
||||
ConfigManagerError::io("failed to read configuration layers", err)
|
||||
})?,
|
||||
};
|
||||
})
|
||||
})
|
||||
.transpose()?;
|
||||
let layers = self
|
||||
.load_config_layers(cwd)
|
||||
.await
|
||||
.map_err(|err| ConfigManagerError::io("failed to read configuration layers", err))?;
|
||||
Self::config_read_response(&layers, params.include_layers)
|
||||
}
|
||||
|
||||
pub(super) fn config_read_response(
|
||||
layers: &ConfigLayerStack,
|
||||
include_layers: bool,
|
||||
) -> Result<ConfigReadResponse, ConfigManagerError> {
|
||||
let mut effective_config_toml = ConfigToml::try_from(layers.effective_config())
|
||||
.map_err(|err| ConfigManagerError::toml("invalid configuration", err))?;
|
||||
effective_config_toml.apps = apps_config_from_layer_stack(&layers);
|
||||
effective_config_toml.apps = apps_config_from_layer_stack(layers);
|
||||
layers
|
||||
.requirements_toml()
|
||||
.apply_exact_to_config(&mut effective_config_toml);
|
||||
@@ -175,7 +180,7 @@ impl ConfigManager {
|
||||
.is_none()
|
||||
});
|
||||
origins.retain(|path, _| !path.starts_with("apps."));
|
||||
origins.extend(apps_config_origins_from_layer_stack(&layers));
|
||||
origins.extend(apps_config_origins_from_layer_stack(layers));
|
||||
origins.retain(|_, metadata| {
|
||||
!matches!(&metadata.name, ConfigLayerSource::PackagedDefaults { .. })
|
||||
});
|
||||
@@ -186,7 +191,7 @@ impl ConfigManager {
|
||||
.into_iter()
|
||||
.map(|(path, metadata)| (path, config_layer_metadata_to_api(metadata)))
|
||||
.collect(),
|
||||
layers: params.include_layers.then(|| {
|
||||
layers: include_layers.then(|| {
|
||||
layers
|
||||
.all_layers_high_to_low()
|
||||
.filter(|layer| {
|
||||
|
||||
@@ -115,9 +115,18 @@ impl ConfigRequestProcessor {
|
||||
&self,
|
||||
params: ConfigReadParams,
|
||||
) -> Result<ConfigReadResponse, JSONRPCErrorError> {
|
||||
let fallback_cwd = params.cwd.as_ref().map(PathBuf::from);
|
||||
let mut response = self.config_manager.read(params).await.map_err(map_error)?;
|
||||
let config = self.load_latest_config(fallback_cwd).await?;
|
||||
let cwd = params.cwd.as_ref().map(PathBuf::from);
|
||||
let config = match self.load_latest_config(cwd).await {
|
||||
Ok(config) => config,
|
||||
Err(err) => {
|
||||
// Report layer read/decoding errors before full-config validation errors, as before.
|
||||
self.config_manager.read(params).await.map_err(map_error)?;
|
||||
return Err(err);
|
||||
}
|
||||
};
|
||||
let mut response =
|
||||
ConfigManager::config_read_response(&config.config_layer_stack, params.include_layers)
|
||||
.map_err(map_error)?;
|
||||
for feature_key in SUPPORTED_EXPERIMENTAL_FEATURE_ENABLEMENT {
|
||||
let Some(feature) = feature_for_key(feature_key) else {
|
||||
continue;
|
||||
|
||||
@@ -854,6 +854,33 @@ metrics_exporter = { otlp-http = { endpoint = "http://127.0.0.1:1/v1/metrics", p
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn config_read_with_relative_cwd_picks_up_external_edits() -> Result<()> {
|
||||
let home = TempDir::new()?;
|
||||
write_config(&home, r#"model = "before""#)?;
|
||||
let mut server = TestAppServer::builder()
|
||||
.with_codex_home(home.path())
|
||||
.without_auto_env()
|
||||
.build_initialized_with_timeout(DEFAULT_READ_TIMEOUT)
|
||||
.await?;
|
||||
|
||||
let mut models = Vec::new();
|
||||
for model in ["before", "after"] {
|
||||
write_config(&home, &format!(r#"model = "{model}""#))?;
|
||||
let id = server
|
||||
.send_config_read_request(ConfigReadParams {
|
||||
include_layers: false,
|
||||
cwd: Some(".".into()),
|
||||
})
|
||||
.await?;
|
||||
let response: ConfigReadResponse =
|
||||
timeout(DEFAULT_READ_TIMEOUT, server.read_response(id)).await??;
|
||||
models.push(response.config.model);
|
||||
}
|
||||
assert_eq!(models, [Some("before".into()), Some("after".into())]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn config_read_returns_effective_and_layers() -> Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
|
||||
Reference in New Issue
Block a user