diff --git a/codex-rs/core/src/config.rs b/codex-rs/core/src/config.rs index 081306dab1..a8ce523095 100644 --- a/codex-rs/core/src/config.rs +++ b/codex-rs/core/src/config.rs @@ -29,6 +29,9 @@ use toml::Value as TomlValue; /// the context window. pub(crate) const PROJECT_DOC_MAX_BYTES: usize = 32 * 1024; // 32 KiB +/// Candidate filenames for user-provided instructions stored in CODEX_HOME. +pub(crate) const USER_INSTRUCTIONS_CANDIDATE_FILENAMES: &[&str] = &["AGENTS.md"]; + /// Application configuration loaded from disk and merged with overrides. #[derive(Debug, Clone, PartialEq)] pub struct Config { @@ -576,13 +579,9 @@ impl Config { } fn load_instructions(codex_dir: Option<&Path>) -> Option { - let mut p = match codex_dir { - Some(p) => p.to_path_buf(), - None => return None, - }; - - p.push("AGENTS.md"); - std::fs::read_to_string(&p).ok().and_then(|s| { + let dir = codex_dir?; + let path = find_user_instructions_path(dir)?; + std::fs::read_to_string(&path).ok().and_then(|s| { let s = s.trim(); if s.is_empty() { None @@ -674,6 +673,20 @@ pub fn log_dir(cfg: &Config) -> std::io::Result { Ok(p) } +/// Returns the path to the first non-empty user instructions file inside the +/// provided CODEX_HOME directory, if any. +pub(crate) fn find_user_instructions_path(codex_dir: &Path) -> Option { + for name in USER_INSTRUCTIONS_CANDIDATE_FILENAMES { + let candidate = codex_dir.join(name); + if let Ok(s) = std::fs::read_to_string(&candidate) { + if !s.trim().is_empty() { + return Some(candidate); + } + } + } + None +} + #[cfg(test)] mod tests { #![allow(clippy::expect_used, clippy::unwrap_used)] diff --git a/codex-rs/core/src/project_doc.rs b/codex-rs/core/src/project_doc.rs index 37c954ac16..244b3f532d 100644 --- a/codex-rs/core/src/project_doc.rs +++ b/codex-rs/core/src/project_doc.rs @@ -12,6 +12,7 @@ //! exists, the search stops – we do **not** walk past the Git root. use crate::config::Config; +use crate::config::find_user_instructions_path; use std::path::Path; use std::path::PathBuf; use tokio::io::AsyncReadExt; @@ -51,41 +52,12 @@ pub(crate) async fn get_user_instructions(config: &Config) -> Option { /// `Err` so callers can decide how to handle them. async fn find_project_doc(config: &Config) -> std::io::Result> { let max_bytes = config.project_doc_max_bytes; - - // Attempt to load from the working directory first. - if let Some(doc) = load_first_candidate(&config.cwd, CANDIDATE_FILENAMES, max_bytes).await? { - return Ok(Some(doc)); + if max_bytes == 0 { + return Ok(None); } - // Walk up towards the filesystem root, stopping once we encounter the Git - // repository root. The presence of **either** a `.git` *file* or - // *directory* counts. - let mut dir = config.cwd.clone(); - - // Canonicalize the path so that we do not end up in an infinite loop when - // `cwd` contains `..` components. - if let Ok(canon) = dir.canonicalize() { - dir = canon; - } - - while let Some(parent) = dir.parent() { - // `.git` can be a *file* (for worktrees or submodules) or a *dir*. - let git_marker = dir.join(".git"); - let git_exists = match tokio::fs::metadata(&git_marker).await { - Ok(_) => true, - Err(e) if e.kind() == std::io::ErrorKind::NotFound => false, - Err(e) => return Err(e), - }; - - if git_exists { - // We are at the repo root – attempt one final load. - if let Some(doc) = load_first_candidate(&dir, CANDIDATE_FILENAMES, max_bytes).await? { - return Ok(Some(doc)); - } - break; - } - - dir = parent.to_path_buf(); + if let Some(path) = find_project_doc_path_async(config).await { + return read_file_with_limit(&path, max_bytes).await; } Ok(None) @@ -108,14 +80,7 @@ pub struct InstructionsInfo { /// path will be `None` even if a file exists. /// - Empty files are treated as "not set". pub async fn collect_instructions_info(config: &Config) -> InstructionsInfo { - let user_instructions_path = { - let mut p = config.codex_home.clone(); - p.push("AGENTS.md"); - match tokio::fs::read_to_string(&p).await { - Ok(s) if !s.trim().is_empty() => Some(p), - _ => None, - } - }; + let user_instructions_path = find_user_instructions_path(&config.codex_home); let project_instructions_path = if config.project_doc_max_bytes == 0 { None @@ -171,14 +136,7 @@ async fn find_first_candidate_path_async(dir: &Path, names: &[&str]) -> Option

InstructionsInfo { - let user_instructions_path = { - let mut p = config.codex_home.clone(); - p.push("AGENTS.md"); - match std::fs::read_to_string(&p) { - Ok(s) if !s.trim().is_empty() => Some(p), - _ => None, - } - }; + let user_instructions_path = find_user_instructions_path(&config.codex_home); let project_instructions_path = if config.project_doc_max_bytes == 0 { None @@ -228,46 +186,32 @@ fn find_first_candidate_path_sync(dir: &Path, names: &[&str]) -> Option None } -/// Attempt to load the first candidate file found in `dir`. Returns the file -/// contents (truncated if it exceeds `max_bytes`) when successful. -async fn load_first_candidate( - dir: &Path, - names: &[&str], - max_bytes: usize, -) -> std::io::Result> { - for name in names { - let candidate = dir.join(name); +/// Read a file with a maximum byte limit; empty content returns None. +async fn read_file_with_limit(path: &Path, max_bytes: usize) -> std::io::Result> { + let file = match tokio::fs::File::open(path).await { + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(e) => return Err(e), + Ok(f) => f, + }; - let file = match tokio::fs::File::open(&candidate).await { - Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue, - Err(e) => return Err(e), - Ok(f) => f, - }; + let size = file.metadata().await?.len(); + let reader = tokio::io::BufReader::new(file); + let mut data = Vec::with_capacity(std::cmp::min(size as usize, max_bytes)); + let mut limited = reader.take(max_bytes as u64); + limited.read_to_end(&mut data).await?; - let size = file.metadata().await?.len(); - - let reader = tokio::io::BufReader::new(file); - let mut data = Vec::with_capacity(std::cmp::min(size as usize, max_bytes)); - let mut limited = reader.take(max_bytes as u64); - limited.read_to_end(&mut data).await?; - - if size as usize > max_bytes { - tracing::warn!( - "Project doc `{}` exceeds {max_bytes} bytes - truncating.", - candidate.display(), - ); - } - - let contents = String::from_utf8_lossy(&data).to_string(); - if contents.trim().is_empty() { - // Empty file – treat as not found. - continue; - } - - return Ok(Some(contents)); + if size as usize > max_bytes { + tracing::warn!( + "Project doc `{}` exceeds {max_bytes} bytes - truncating.", + path.display(), + ); } - Ok(None) + let contents = String::from_utf8_lossy(&data).to_string(); + if contents.trim().is_empty() { + return Ok(None); + } + Ok(Some(contents)) } #[cfg(test)]