Skip to content

Commit 13c000d

Browse files
committed
refactor: DRY; surface truncated AGENTS.md status via shared project-doc helper
1 parent 626b1d0 commit 13c000d

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

codex-rs/core/src/project_doc.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
1616
use crate::config::Config;
1717
use dunce::canonicalize as normalize_path;
18+
use std::fs;
1819
use std::path::PathBuf;
1920
use tokio::io::AsyncReadExt;
2021
use tracing::error;
@@ -178,6 +179,36 @@ pub fn discover_project_doc_paths(config: &Config) -> std::io::Result<Vec<PathBu
178179
Ok(found)
179180
}
180181

182+
/// Returns `true` when the concatenated docs would exceed `max_bytes`, matching
183+
/// the truncation logic used while embedding project documentation.
184+
pub fn docs_truncated(paths: &[PathBuf], max_bytes: usize) -> bool {
185+
if paths.is_empty() {
186+
return false;
187+
}
188+
if max_bytes == 0 {
189+
return true;
190+
}
191+
192+
let mut remaining = max_bytes as u64;
193+
for path in paths {
194+
if remaining == 0 {
195+
return true;
196+
}
197+
let size = match fs::metadata(path) {
198+
Ok(meta) => meta.len(),
199+
Err(_) => continue,
200+
};
201+
if size == 0 {
202+
continue;
203+
}
204+
if size > remaining {
205+
return true;
206+
}
207+
remaining = remaining.saturating_sub(size);
208+
}
209+
false
210+
}
211+
181212
fn candidate_filenames<'a>(config: &'a Config) -> Vec<&'a str> {
182213
let mut names: Vec<&'a str> =
183214
Vec::with_capacity(2 + config.project_doc_fallback_filenames.len());

codex-rs/tui/src/status/helpers.rs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ use codex_app_server_protocol::AuthMode;
66
use codex_core::AuthManager;
77
use codex_core::config::Config;
88
use codex_core::project_doc::discover_project_doc_paths;
9-
use std::fs;
9+
use codex_core::project_doc::docs_truncated;
1010
use std::path::Path;
11-
use std::path::PathBuf;
1211
use unicode_width::UnicodeWidthStr;
1312

1413
use super::account::StatusAccountDisplay;
@@ -89,40 +88,6 @@ pub(crate) fn compose_agents_summary(config: &Config) -> (String, bool) {
8988
}
9089
}
9190

92-
/// Replicates the truncation logic in `project_doc::read_project_docs` by
93-
/// walking the resolved doc paths and tracking how much of the configured
94-
/// budget remains. Returns `true` when any file would exceed the budget (or
95-
/// when the budget is zero but docs exist), signaling that the TUI should warn
96-
/// the user in `/status`.
97-
fn docs_truncated(paths: &[PathBuf], max_bytes: usize) -> bool {
98-
if paths.is_empty() {
99-
return false;
100-
}
101-
if max_bytes == 0 {
102-
// Docs exist but embedding disabled entirely.
103-
return true;
104-
}
105-
106-
let mut remaining = max_bytes as u64;
107-
for path in paths {
108-
if remaining == 0 {
109-
return true;
110-
}
111-
let size = match fs::metadata(path) {
112-
Ok(meta) => meta.len(),
113-
Err(_) => continue,
114-
};
115-
if size == 0 {
116-
continue;
117-
}
118-
if size > remaining {
119-
return true;
120-
}
121-
remaining = remaining.saturating_sub(size);
122-
}
123-
false
124-
}
125-
12691
pub(crate) fn compose_account_display(auth_manager: &AuthManager) -> Option<StatusAccountDisplay> {
12792
let auth = auth_manager.auth()?;
12893

0 commit comments

Comments
 (0)