Files
codex/codex-rs/config/src/state_tests.rs
jif-oai deedf3b2c4 feat: add layered --profile-v2 config files (#17141)
## Why

`--profile-v2 <name>` gives launchers and runtime entry points a named
profile config without making each profile duplicate the base user
config. The base `$CODEX_HOME/config.toml` still loads first, then
`$CODEX_HOME/<name>.config.toml` layers above it and becomes the active
writable user config for that session.

That keeps shared defaults, plugin/MCP setup, and managed/user
constraints in one place while letting a named profile override only the
pieces that need to differ.

## What Changed

- Added the shared `--profile-v2 <name>` runtime option with validated
plain names, now represented by `ProfileV2Name`.
- Extended config layer state so the base user config and selected
profile config are both `User` layers; APIs expose the active user layer
and merged effective user config.
- Threaded profile selection through runtime entry points: `codex`,
`codex exec`, `codex review`, `codex resume`, `codex fork`, and `codex
debug prompt-input`.
- Made user-facing config writes go to the selected profile file when
active, including TUI/settings persistence, app-server config writes,
and MCP/app tool approval persistence.
- Made plugin, marketplace, MCP, hooks, and config reload paths read
from the merged user config so base and profile layers both participate.
- Updated app-server config layer schemas to mark profile-backed user
layers.

## Limits

`--profile-v2` is still rejected for config-management subcommands such
as feature, MCP, and marketplace edits. Those paths remain tied to the
base `config.toml` until they have explicit profile-selection semantics.

Some adjacent background writes may still update base or global state
rather than the selected profile:

- marketplace auto-upgrade metadata
- automatic MCP dependency installs from skills
- remote plugin sync or uninstall config edits
- personality migration marker/default writes

## Verification

Added targeted coverage for profile name validation, layer
ordering/merging, selected-profile writes, app-server config writes,
session hot reload, plugin config merging, hooks/config fixture updates,
and MCP/app approval persistence.

---------

Co-authored-by: Codex <noreply@openai.com>
2026-05-14 15:16:15 +02:00

142 lines
4.2 KiB
Rust

use super::*;
use pretty_assertions::assert_eq;
use tempfile::TempDir;
fn test_user_config_path(temp_dir: &TempDir, file_name: &str) -> AbsolutePathBuf {
AbsolutePathBuf::from_absolute_path(temp_dir.path().join(file_name))
.expect("test user config path should be absolute")
}
#[test]
fn origins_use_canonical_key_aliases() {
let layer = ConfigLayerEntry::new(
ConfigLayerSource::SessionFlags,
toml::from_str(
r#"
[memories]
no_memories_if_mcp_or_web_search = true
"#,
)
.expect("config TOML should parse"),
);
let metadata = layer.metadata();
let stack = ConfigLayerStack::new(
vec![layer],
ConfigRequirements::default(),
ConfigRequirementsToml::default(),
)
.expect("single layer stack should be valid");
let origins = stack.origins();
assert_eq!(
origins.get("memories.disable_on_external_context"),
Some(&metadata)
);
assert!(
!origins.contains_key("memories.no_memories_if_mcp_or_web_search"),
"legacy key should be canonicalized before origin recording"
);
}
#[test]
fn active_user_layer_is_highest_precedence_user_layer() {
let temp_dir = TempDir::new().expect("tempdir");
let base_file = test_user_config_path(&temp_dir, "config.toml");
let profile_file = test_user_config_path(&temp_dir, "work.config.toml");
let base_layer = ConfigLayerEntry::new(
ConfigLayerSource::User {
file: base_file,
profile: None,
},
toml::from_str(
r#"
model = "base"
approval_policy = "on-failure"
"#,
)
.expect("base config"),
);
let profile_layer = ConfigLayerEntry::new(
ConfigLayerSource::User {
file: profile_file.clone(),
profile: Some("work".to_string()),
},
toml::from_str(r#"model = "profile""#).expect("profile config"),
);
let stack = ConfigLayerStack::new(
vec![base_layer, profile_layer],
ConfigRequirements::default(),
ConfigRequirementsToml::default(),
)
.expect("multiple user layers should be valid");
assert_eq!(stack.get_user_config_file(), Some(&profile_file));
assert_eq!(
stack
.effective_user_config()
.expect("merged user config")
.get("model")
.and_then(toml::Value::as_str),
Some("profile")
);
assert_eq!(
stack
.effective_user_config()
.expect("merged user config")
.get("approval_policy")
.and_then(toml::Value::as_str),
Some("on-failure")
);
}
#[test]
fn with_user_config_updates_matching_user_layer_without_replacing_active_profile() {
let temp_dir = TempDir::new().expect("tempdir");
let base_file = test_user_config_path(&temp_dir, "config.toml");
let profile_file = test_user_config_path(&temp_dir, "work.config.toml");
let base_layer = ConfigLayerEntry::new(
ConfigLayerSource::User {
file: base_file.clone(),
profile: None,
},
toml::from_str(r#"model = "base""#).expect("base config"),
);
let profile_layer = ConfigLayerEntry::new(
ConfigLayerSource::User {
file: profile_file.clone(),
profile: Some("work".to_string()),
},
toml::from_str(r#"approval_policy = "on-failure""#).expect("profile config"),
);
let stack = ConfigLayerStack::new(
vec![base_layer, profile_layer],
ConfigRequirements::default(),
ConfigRequirementsToml::default(),
)
.expect("multiple user layers should be valid");
let updated = stack.with_user_config(
&base_file,
toml::from_str(r#"model = "updated-base""#).expect("updated base config"),
);
assert_eq!(updated.get_user_config_file(), Some(&profile_file));
assert_eq!(
updated
.effective_user_config()
.expect("merged user config")
.get("model")
.and_then(toml::Value::as_str),
Some("updated-base")
);
assert_eq!(
updated
.effective_user_config()
.expect("merged user config")
.get("approval_policy")
.and_then(toml::Value::as_str),
Some("on-failure")
);
}