From 79ebe236234e7964f337b932f15ed050d14806cd Mon Sep 17 00:00:00 2001 From: pap Date: Wed, 6 Aug 2025 23:36:02 +0100 Subject: [PATCH] less code/comments --- codex-rs/common/src/config_summary.rs | 7 +- codex-rs/core/src/project_doc.rs | 126 ++++++++++++-------------- 2 files changed, 62 insertions(+), 71 deletions(-) diff --git a/codex-rs/common/src/config_summary.rs b/codex-rs/common/src/config_summary.rs index 0d86db330a..616030a85a 100644 --- a/codex-rs/common/src/config_summary.rs +++ b/codex-rs/common/src/config_summary.rs @@ -6,9 +6,8 @@ use crate::sandbox_summary::summarize_sandbox_policy; /// Build a list of key/value pairs summarizing the effective configuration. pub fn create_config_summary_entries(config: &Config) -> Vec<(&'static str, String)> { - let mut entries = vec![("workdir", config.cwd.display().to_string())]; - - entries.extend([ + let mut entries = vec![ + ("workdir", config.cwd.display().to_string()), ( "agents.md", agents_doc_path_string(config).unwrap_or_else(|| "none".to_string()), @@ -17,7 +16,7 @@ pub fn create_config_summary_entries(config: &Config) -> Vec<(&'static str, Stri ("provider", config.model_provider_id.clone()), ("approval", config.approval_policy.to_string()), ("sandbox", summarize_sandbox_policy(&config.sandbox_policy)), - ]); + ]; if config.model_provider.wire_api == WireApi::Responses && config.model_family.supports_reasoning_summaries { diff --git a/codex-rs/core/src/project_doc.rs b/codex-rs/core/src/project_doc.rs index 328e15ae05..c56083249f 100644 --- a/codex-rs/core/src/project_doc.rs +++ b/codex-rs/core/src/project_doc.rs @@ -24,9 +24,64 @@ const CANDIDATE_FILENAMES: &[&str] = &["AGENTS.md"]; /// be concatenated with the following separator. const PROJECT_DOC_SEPARATOR: &str = "\n\n--- project-doc ---\n\n"; +pub(crate) async fn get_user_instructions(config: &Config) -> Option { + match find_project_doc(config).await { + Ok(Some(project_doc)) => match &config.user_instructions { + Some(original_instructions) => Some(format!( + "{original_instructions}{PROJECT_DOC_SEPARATOR}{project_doc}" + )), + None => Some(project_doc), + }, + Ok(None) => config.user_instructions.clone(), + Err(e) => { + error!("error trying to find project doc: {e:#}"); + config.user_instructions.clone() + } + } +} + + +/// Attempt to locate and load the project documentation. Currently, the search +/// starts from `Config::cwd`, but if we may want to consider other directories +/// in the future, e.g., additional writable directories in the `SandboxPolicy`. +/// +/// On success returns `Ok(Some(contents))`. If no documentation file is found +/// the function returns `Ok(None)`. Unexpected I/O failures bubble up as +/// `Err` so callers can decide how to handle them. +async fn find_project_doc(config: &Config) -> std::io::Result> { + use tokio::io::BufReader; + + let Some(path) = discover_project_doc_path(config)? else { + return Ok(None); + }; + + let max_bytes = config.project_doc_max_bytes; + + let file = tokio::fs::File::open(&path).await?; + let size = file.metadata().await?.len() as usize; + + let reader = BufReader::new(file); + let mut data = Vec::with_capacity(std::cmp::min(size, max_bytes)); + let mut limited = reader.take(max_bytes as u64); + limited.read_to_end(&mut data).await?; + + if size > max_bytes { + tracing::warn!( + "Project doc `{}` exceeds {max_bytes} bytes - truncating.", + path.display(), + ); + } + + let contents = String::from_utf8_lossy(&data).to_string(); + if contents.trim().is_empty() { + return Ok(None); + } + + Ok(Some(contents)) +} + /// Public helper that returns the discovered AGENTS.md path. -/// Returns `Ok(None)` when no suitable file is found or -/// `project_doc_max_bytes == 0`. +/// Returns `Ok(None)` when no suitable file is found or `project_doc_max_bytes == 0`. pub fn discover_project_doc_path(config: &Config) -> std::io::Result> { if config.project_doc_max_bytes == 0 { return Ok(None); @@ -75,24 +130,6 @@ fn discover_project_doc_path_from_dir( Ok(None) } -/// Combines `Config::instructions` and `AGENTS.md` (if present) into a single -/// string of instructions. -pub(crate) async fn get_user_instructions(config: &Config) -> Option { - match find_project_doc(config).await { - Ok(Some(project_doc)) => match &config.user_instructions { - Some(original_instructions) => Some(format!( - "{original_instructions}{PROJECT_DOC_SEPARATOR}{project_doc}" - )), - None => Some(project_doc), - }, - Ok(None) => config.user_instructions.clone(), - Err(e) => { - error!("error trying to find project doc: {e:#}"); - config.user_instructions.clone() - } - } -} - /// Return a human‑readable description of the AGENTS.md path(s) that will be /// loaded for this session, or `None` if neither global nor project docs are /// present. @@ -125,8 +162,6 @@ pub fn agents_doc_path_string(config: &Config) -> Option { } } -/// Return the first path in `dir` that matches any of `names` and is non‑empty -/// (after trimming). Returns `None` if no such file exists. fn first_nonempty_candidate_in_dir(dir: &Path, names: &[&str]) -> Option { for name in names { let candidate = dir.join(name); @@ -141,12 +176,8 @@ fn first_nonempty_candidate_in_dir(dir: &Path, names: &[&str]) -> Option f, @@ -170,45 +201,6 @@ fn first_nonempty_candidate_in_dir(dir: &Path, names: &[&str]) -> Option std::io::Result> { - use tokio::io::BufReader; - - let Some(path) = discover_project_doc_path(config)? else { - return Ok(None); - }; - - let max_bytes = config.project_doc_max_bytes; - - let file = tokio::fs::File::open(&path).await?; - let size = file.metadata().await?.len() as usize; - - let reader = BufReader::new(file); - let mut data = Vec::with_capacity(std::cmp::min(size, max_bytes)); - let mut limited = reader.take(max_bytes as u64); - limited.read_to_end(&mut data).await?; - - if size > max_bytes { - tracing::warn!( - "Project doc `{}` exceeds {max_bytes} bytes - truncating.", - path.display(), - ); - } - - let contents = String::from_utf8_lossy(&data).to_string(); - if contents.trim().is_empty() { - return Ok(None); - } - - Ok(Some(contents)) -} - #[cfg(test)] mod tests { #![allow(clippy::expect_used, clippy::unwrap_used)]