mirror of
https://github.com/openai/codex.git
synced 2026-11-20 23:08:21 +00:00
Use root repo hooks in linked worktrees (#21969)
# Why Linked worktrees currently load their own project hook declarations, so the same repo can present different hook definitions depending on which checkout is active. https://github.com/openai/codex/pull/21762 tried to share trust by giving matching worktree hooks a shared synthetic key, but review pointed out that divergent worktree hook definitions would then fight over one `trusted_hash`. Instead of introducing a second trust model, this makes linked worktrees use the root checkout as the single source of truth for project hook declarations. Worktree-local project config can still diverge for unrelated settings, but project hooks now keep one real source path and one trust state per repo. # What - Teach project config loading to remember the matching root-checkout `.codex/` folder for actual linked-worktree project layers. - Keep ordinary project config sourced from the worktree, but replace project hook declarations with the root checkout's matching layer before hook discovery runs, including linked-worktree layers with `.codex/` but no local `config.toml`. - Make hook discovery use that authoritative hook folder for both `hooks.json` and TOML hook source paths, so linked worktrees produce the same hook key and trust state as the root checkout. - Cover the linked-worktree path plus regressions for missing worktree `config.toml` and nested non-worktree project roots.
This commit is contained in:
1 parent
2304ec45ca
commit
934a40c7d9
6 files changed
+567
-39
No files matched your search
@@ -627,6 +627,8 @@ struct ProjectTrustContext {
|
||||
project_root: AbsolutePathBuf,
|
||||
project_root_key: String,
|
||||
project_root_lookup_keys: Vec<String>,
|
||||
checkout_root: Option<AbsolutePathBuf>,
|
||||
repo_root: Option<AbsolutePathBuf>,
|
||||
repo_root_key: Option<String>,
|
||||
repo_root_lookup_keys: Option<Vec<String>>,
|
||||
projects_trust: std::collections::HashMap<String, TrustLevel>,
|
||||
@@ -712,22 +714,36 @@ impl ProjectTrustContext {
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn root_checkout_hooks_folder_for_dir(&self, dir: &AbsolutePathBuf) -> Option<AbsolutePathBuf> {
|
||||
let checkout_root = self.checkout_root.as_ref()?;
|
||||
let repo_root = self.repo_root.as_ref()?;
|
||||
// Regular checkouts resolve both paths to the same root; linked worktrees do not.
|
||||
if checkout_root == repo_root {
|
||||
return None;
|
||||
}
|
||||
|
||||
let relative_dir = dir.as_path().strip_prefix(checkout_root.as_path()).ok()?;
|
||||
Some(repo_root.join(relative_dir).join(".codex"))
|
||||
}
|
||||
}
|
||||
|
||||
fn project_layer_entry(
|
||||
dot_codex_folder: &AbsolutePathBuf,
|
||||
config: TomlValue,
|
||||
disabled_reason: Option<String>,
|
||||
hooks_config_folder_override: Option<AbsolutePathBuf>,
|
||||
) -> ConfigLayerEntry {
|
||||
let source = ConfigLayerSource::Project {
|
||||
dot_codex_folder: dot_codex_folder.clone(),
|
||||
};
|
||||
|
||||
if let Some(reason) = disabled_reason {
|
||||
let entry = if let Some(reason) = disabled_reason {
|
||||
ConfigLayerEntry::new_disabled(source, config, reason)
|
||||
} else {
|
||||
ConfigLayerEntry::new(source, config)
|
||||
}
|
||||
};
|
||||
entry.with_hooks_config_folder_override(hooks_config_folder_override)
|
||||
}
|
||||
|
||||
fn sanitize_project_config(config: &mut TomlValue) -> Vec<String> {
|
||||
@@ -786,6 +802,7 @@ async fn project_trust_context(
|
||||
.first()
|
||||
.cloned()
|
||||
.unwrap_or_else(|| project_trust_key(project_root.as_path()));
|
||||
let checkout_root = find_git_checkout_root(fs, cwd).await;
|
||||
let repo_root = resolve_root_git_project_for_trust(fs, cwd).await;
|
||||
let repo_root_lookup_keys = repo_root
|
||||
.as_ref()
|
||||
@@ -803,6 +820,8 @@ async fn project_trust_context(
|
||||
project_root,
|
||||
project_root_key,
|
||||
project_root_lookup_keys,
|
||||
checkout_root,
|
||||
repo_root,
|
||||
repo_root_key,
|
||||
repo_root_lookup_keys,
|
||||
projects_trust,
|
||||
@@ -944,6 +963,24 @@ async fn find_project_root(
|
||||
Ok(cwd.clone())
|
||||
}
|
||||
|
||||
async fn find_git_checkout_root(
|
||||
fs: &dyn ExecutorFileSystem,
|
||||
cwd: &AbsolutePathBuf,
|
||||
) -> Option<AbsolutePathBuf> {
|
||||
let base = match fs.get_metadata(cwd, /*sandbox*/ None).await {
|
||||
Ok(metadata) if metadata.is_directory => cwd.clone(),
|
||||
_ => cwd.parent()?,
|
||||
};
|
||||
|
||||
for dir in base.ancestors() {
|
||||
let dot_git = dir.join(".git");
|
||||
if fs.get_metadata(&dot_git, /*sandbox*/ None).await.is_ok() {
|
||||
return Some(dir);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
struct LoadedProjectLayers {
|
||||
layers: Vec<ConfigLayerEntry>,
|
||||
startup_warnings: Vec<String>,
|
||||
@@ -995,6 +1032,7 @@ async fn load_project_layers(
|
||||
|
||||
let decision = trust_context.decision_for_dir(&dir);
|
||||
let disabled_reason = trust_context.disabled_reason_for_decision(&decision);
|
||||
let hooks_config_folder_override = trust_context.root_checkout_hooks_folder_for_dir(&dir);
|
||||
let dot_codex_normalized =
|
||||
normalize_path(dot_codex_abs.as_path()).unwrap_or_else(|_| dot_codex_abs.to_path_buf());
|
||||
if dot_codex_abs == codex_home_abs || dot_codex_normalized == codex_home_normalized {
|
||||
@@ -1019,6 +1057,7 @@ async fn load_project_layers(
|
||||
&dot_codex_abs,
|
||||
TomlValue::Table(toml::map::Map::new()),
|
||||
disabled_reason.clone(),
|
||||
hooks_config_folder_override.clone(),
|
||||
));
|
||||
continue;
|
||||
}
|
||||
@@ -1027,13 +1066,25 @@ async fn load_project_layers(
|
||||
let ignored_project_config_keys = sanitize_project_config(&mut config);
|
||||
let config =
|
||||
resolve_relative_paths_in_config_toml(config, dot_codex_abs.as_path())?;
|
||||
let config = merge_root_checkout_project_hooks(
|
||||
fs,
|
||||
config,
|
||||
hooks_config_folder_override.as_ref(),
|
||||
decision.is_trusted(),
|
||||
)
|
||||
.await?;
|
||||
if disabled_reason.is_none() && !ignored_project_config_keys.is_empty() {
|
||||
startup_warnings.push(project_ignored_config_keys_warning(
|
||||
&dot_codex_abs,
|
||||
&ignored_project_config_keys,
|
||||
));
|
||||
}
|
||||
let entry = project_layer_entry(&dot_codex_abs, config, disabled_reason.clone());
|
||||
let entry = project_layer_entry(
|
||||
&dot_codex_abs,
|
||||
config,
|
||||
disabled_reason.clone(),
|
||||
hooks_config_folder_override.clone(),
|
||||
);
|
||||
layers.push(entry);
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -1041,10 +1092,18 @@ async fn load_project_layers(
|
||||
// If there is no config.toml file, record an empty entry
|
||||
// for this project layer, as this may still have subfolders
|
||||
// that are significant in the overall ConfigLayerStack.
|
||||
let config = merge_root_checkout_project_hooks(
|
||||
fs,
|
||||
TomlValue::Table(toml::map::Map::new()),
|
||||
hooks_config_folder_override.as_ref(),
|
||||
decision.is_trusted(),
|
||||
)
|
||||
.await?;
|
||||
layers.push(project_layer_entry(
|
||||
&dot_codex_abs,
|
||||
TomlValue::Table(toml::map::Map::new()),
|
||||
config,
|
||||
disabled_reason,
|
||||
hooks_config_folder_override,
|
||||
));
|
||||
} else {
|
||||
let config_file_display = config_file.as_path().display();
|
||||
@@ -1062,6 +1121,64 @@ async fn load_project_layers(
|
||||
startup_warnings,
|
||||
})
|
||||
}
|
||||
|
||||
/// For linked worktrees, preserve ordinary worktree-local project config while
|
||||
/// replacing only hook declarations with the matching root-checkout layer.
|
||||
async fn merge_root_checkout_project_hooks(
|
||||
fs: &dyn ExecutorFileSystem,
|
||||
mut config: TomlValue,
|
||||
hooks_config_folder_override: Option<&AbsolutePathBuf>,
|
||||
is_trusted: bool,
|
||||
) -> io::Result<TomlValue> {
|
||||
let Some(hooks_config_folder) = hooks_config_folder_override else {
|
||||
return Ok(config);
|
||||
};
|
||||
let hooks_config_file = hooks_config_folder.join(CONFIG_TOML_FILE);
|
||||
let root_config = match fs
|
||||
.read_file_text(&hooks_config_file, /*sandbox*/ None)
|
||||
.await
|
||||
{
|
||||
Ok(contents) => {
|
||||
let parsed: TomlValue = match toml::from_str(&contents) {
|
||||
Ok(parsed) => parsed,
|
||||
Err(err) => {
|
||||
if is_trusted {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
format!(
|
||||
"Error parsing project hooks config file {}: {err}",
|
||||
hooks_config_file.as_path().display()
|
||||
),
|
||||
));
|
||||
}
|
||||
TomlValue::Table(toml::map::Map::new())
|
||||
}
|
||||
};
|
||||
resolve_relative_paths_in_config_toml(parsed, hooks_config_folder.as_path())?
|
||||
}
|
||||
Err(err) if err.kind() == io::ErrorKind::NotFound => {
|
||||
TomlValue::Table(toml::map::Map::new())
|
||||
}
|
||||
Err(err) => {
|
||||
return Err(io::Error::new(
|
||||
err.kind(),
|
||||
format!(
|
||||
"Failed to read project hooks config file {}: {err}",
|
||||
hooks_config_file.as_path().display()
|
||||
),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let Some(config_table) = config.as_table_mut() else {
|
||||
return Ok(config);
|
||||
};
|
||||
config_table.remove("hooks");
|
||||
if let Some(hooks) = root_config.get("hooks") {
|
||||
config_table.insert("hooks".to_string(), hooks.clone());
|
||||
}
|
||||
Ok(config)
|
||||
}
|
||||
/// The legacy mechanism for specifying admin-enforced configuration is to read
|
||||
/// from a file like `/etc/codex/managed_config.toml` that has the same
|
||||
/// structure as `config.toml` where fields like `approval_policy` can specify
|
||||
|
||||
Reference in new issue
Block a user