Reuse the cached runtime keymap for default configuration (#57876)

## What changed

Return the cached built-in runtime keymap when `TuiKeymap` matches the default configuration, avoiding repeated keymap resolution. Keep custom configurations on the existing resolution and validation path.

## Testing

Add a startup regression test that edits `config.toml` after the directory-trust read and verifies that `thread/start` uses the updated `service_tier`, including with an embedded app server.

GitOrigin-RevId: 0c079c96cb2925c1367782e289595c637612e2cc
This commit is contained in:
Benjamin Carlsson
2026-10-29 01:30:00 +00:00
committed by copyberry
parent edd1ae37ec
commit e75e4af229
2 changed files with 68 additions and 1 deletions
@@ -679,6 +679,66 @@ async fn fresh_startup_reads_destination_and_cleared_model_uses_catalog() -> Res
Ok(())
}
#[tokio::test]
async fn fresh_startup_observes_config_edits_after_directory_trust() -> Result<()> {
let home = tempdir()?;
let destination = tempdir()?;
std::fs::create_dir(destination.path().join(".git"))?;
let config_path = home.path().join("config.toml");
let project = serde_json::to_string(&destination.path().display().to_string())?;
let write_tier = |tier: &str| {
std::fs::write(
&config_path,
format!("service_tier = \"{tier}\"\n[projects.{project}]\ntrust_level = \"trusted\"\n"),
)
};
write_tier("priority")?;
let config = ConfigBuilder::default()
.codex_home(home.path().to_path_buf())
.loader_overrides(LoaderOverrides::without_managed_config_for_tests())
.harness_overrides(ConfigOverrides {
cwd: Some(destination.path().to_path_buf()),
..Default::default()
})
.build()
.await?;
let (mut server, requests, proxy) = start_recording_app_server_with_history(
&config,
HistoryCapabilities::Current,
/*blocked_thread_list*/ None,
/*failed_thread_name*/ None,
crate::app_server_session::ThreadParamsMode::Embedded,
LoaderOverrides {
user_config_path: Some(config_path.clone().abs()),
..LoaderOverrides::default()
},
)
.await?;
let bootstrap = server.bootstrap(&config).await?;
let reads_before = recorded_params(&requests, "config/read").len();
let trust = crate::config_update::read_remote_project_trust(
server.request_handle(),
config.cwd.as_path(),
crate::config_update::ProjectTrustHost::Local,
)
.await?;
assert_eq!(trust, None);
// A different process can edit this file even when the app server is embedded.
write_tier("flex")?;
run_until_thread_start(server, config, bootstrap, &requests).await?;
let starts = recorded_params(&requests, "thread/start");
assert_eq!(
(
starts.len(),
&starts[0]["serviceTier"],
recorded_params(&requests, "config/read").len() - reads_before,
),
(1, &serde_json::json!("flex"), 2),
);
proxy.abort();
Ok(())
}
#[tokio::test]
async fn fresh_startup_falls_back_only_for_unsupported_config_read() -> Result<()> {
for capabilities in [
+8 -1
View File
@@ -652,7 +652,7 @@ impl RuntimeKeymap {
DEFAULTS
.get_or_init(|| {
Self::from_config(&TuiKeymap::default()).unwrap_or_else(|error| {
Self::resolve(&TuiKeymap::default()).unwrap_or_else(|error| {
panic!("built-in keymap defaults must be valid: {error}")
})
})
@@ -670,6 +670,13 @@ impl RuntimeKeymap {
/// Calling code should not merge bindings across unrelated contexts before
/// dispatch, or conflict guarantees from this resolver no longer hold.
pub(crate) fn from_config(keymap: &TuiKeymap) -> Result<Self, String> {
if keymap == &TuiKeymap::default() {
return Ok(Self::defaults());
}
Self::resolve(keymap)
}
fn resolve(keymap: &TuiKeymap) -> Result<Self, String> {
let defaults = Self::built_in_defaults();
let chords = Arc::new(RuntimeChordKeymap::from_config(keymap)?);
let side_toggle_default_is_shadowed = keymap.global.toggle_side_conversation.is_none()