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>
This commit is contained in:
jif-oaiandCodex authored and GitHub committed 2026-05-14 15:16:15 +02:00
1 parent 17cd321c32
commit deedf3b2c4
55 files changed
+1302 -241

No files matched your search

+39 -11
View File
@@ -1037,13 +1037,21 @@ pub fn apply_blocking(
codex_home: &Path,
profile: Option<&str>,
edits: &[ConfigEdit],
) -> anyhow::Result<()> {
let config_path = codex_home.join(CONFIG_TOML_FILE);
apply_blocking_to_resolved_file(&config_path, profile, edits)
}
fn apply_blocking_to_resolved_file(
resolved_config_file: &Path,
legacy_profile: Option<&str>,
edits: &[ConfigEdit],
) -> anyhow::Result<()> {
if edits.is_empty() {
return Ok(());
}
let config_path = codex_home.join(CONFIG_TOML_FILE);
let write_paths = resolve_symlink_write_paths(&config_path)?;
let write_paths = resolve_symlink_write_paths(resolved_config_file)?;
let serialized = match write_paths.read_path {
Some(path) => match std::fs::read_to_string(&path) {
Ok(contents) => contents,
@@ -1059,7 +1067,7 @@ pub fn apply_blocking(
serialized.parse::<DocumentMut>()?
};
let profile = profile.map(ToOwned::to_owned).or_else(|| {
let profile = legacy_profile.map(ToOwned::to_owned).or_else(|| {
doc.get("profile")
.and_then(|item| item.as_str())
.map(ToOwned::to_owned)
@@ -1078,7 +1086,7 @@ pub fn apply_blocking(
write_atomically(&write_paths.write_path, &document.doc.to_string()).with_context(|| {
format!(
"failed to persist config.toml at {}",
"failed to persist config at {}",
write_paths.write_path.display()
)
})?;
@@ -1087,30 +1095,50 @@ pub fn apply_blocking(
}
/// Persist edits asynchronously by offloading the blocking writer.
///
/// `profile` selects a legacy `[profiles.<name>]` section inside
/// `$CODEX_HOME/config.toml`; profile-v2 callers should resolve their target
/// file before constructing a [ConfigEditsBuilder].
pub async fn apply(
codex_home: &Path,
profile: Option<&str>,
edits: Vec<ConfigEdit>,
) -> anyhow::Result<()> {
let codex_home = codex_home.to_path_buf();
let config_path = codex_home.join(CONFIG_TOML_FILE);
let profile = profile.map(ToOwned::to_owned);
task::spawn_blocking(move || apply_blocking(&codex_home, profile.as_deref(), &edits))
.await
.context("config persistence task panicked")?
task::spawn_blocking(move || {
apply_blocking_to_resolved_file(&config_path, profile.as_deref(), &edits)
})
.await
.context("config persistence task panicked")?
}
/// Fluent builder to batch config edits and apply them atomically.
#[derive(Default)]
pub struct ConfigEditsBuilder {
codex_home: PathBuf,
config_path: PathBuf,
profile: Option<String>,
edits: Vec<ConfigEdit>,
}
impl ConfigEditsBuilder {
pub fn new(codex_home: &Path) -> Self {
Self::for_config_path(&codex_home.join(CONFIG_TOML_FILE))
}
pub fn for_config(config: &crate::config::Config) -> Self {
let config_path = config
.config_layer_stack
.get_user_config_file()
.map(codex_utils_absolute_path::AbsolutePathBuf::to_path_buf)
.unwrap_or_else(|| config.codex_home.join(CONFIG_TOML_FILE).to_path_buf());
Self::for_config_path(&config_path)
}
pub fn for_config_path(config_path: &Path) -> Self {
Self {
codex_home: codex_home.to_path_buf(),
config_path: config_path.to_path_buf(),
profile: None,
edits: Vec::new(),
}
@@ -1369,13 +1397,13 @@ impl ConfigEditsBuilder {
/// Apply edits on a blocking thread.
pub fn apply_blocking(self) -> anyhow::Result<()> {
apply_blocking(&self.codex_home, self.profile.as_deref(), &self.edits)
apply_blocking_to_resolved_file(&self.config_path, self.profile.as_deref(), &self.edits)
}
/// Apply edits asynchronously via a blocking offload.
pub async fn apply(self) -> anyhow::Result<()> {
task::spawn_blocking(move || {
apply_blocking(&self.codex_home, self.profile.as_deref(), &self.edits)
apply_blocking_to_resolved_file(&self.config_path, self.profile.as_deref(), &self.edits)
})
.await
.context("config persistence task panicked")?