Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Reuse compiled search patterns across compressed rollouts (#53732)

Dan Gillespieefa159f7aa3f
diff --git a/codex-rs/rollout/src/search.rs b/codex-rs/rollout/src/search.rs
index 77206404e1..c64f2a3310 100644
--- a/codex-rs/rollout/src/search.rs
+++ b/codex-rs/rollout/src/search.rs
@@ -124,6 +124,7 @@ async fn scan_rollout_matches(
let mut matches = HashMap::new();
let mut dirs = vec![root.to_path_buf()];
let json_search_term = case_insensitive_literal_regex(json_search_term)?;
+ let mut patterns = None;
while let Some(dir) = dirs.pop() {
let mut entries = match tokio::fs::read_dir(dir).await {
@@ -146,7 +147,7 @@ async fn scan_rollout_matches(
};
if rollout_file.is_compressed() {
if let Some(snippet) =
- first_rollout_content_match_snippet(rollout_file.path(), search_term)
+ first_content_match_snippet(rollout_file.path(), search_term, &mut patterns)
.await
.unwrap_or_else(|err| {
tracing::warn!(
@@ -186,10 +187,26 @@ async fn rollout_contains(path: &Path, search_term: &Regex) -> io::Result<bool>
pub async fn first_rollout_content_match_snippet(
path: &Path,
search_term: &str,
+) -> io::Result<Option<String>> {
+ first_content_match_snippet(path, search_term, &mut None).await
+}
+
+async fn first_content_match_snippet(
+ path: &Path,
+ search_term: &str,
+ patterns: &mut Option<(Regex, Regex)>,
) -> io::Result<Option<String>> {
let lines = compression::open_rollout_line_reader(path).await?;
- let json_search_term = case_insensitive_literal_regex(json_escaped_search_term(search_term)?)?;
- let search_term = case_insensitive_literal_regex(search_term)?;
+ // Keep compilation errors inside the caller's per-file error handler.
+ let (json_search_term, search_term) = match patterns {
+ Some(patterns) => patterns,
+ None => patterns.insert((
+ case_insensitive_literal_regex(json_escaped_search_term(search_term)?)?,
+ case_insensitive_literal_regex(search_term)?,
+ )),
+ };
+ let json_search_term = json_search_term.clone();
+ let search_term = search_term.clone();
lines
.find_map(move |line| {
if json_search_term.is_match(line) {
@@ -207,6 +224,7 @@ async fn scan_compressed_rollout_matches(
) -> io::Result<RolloutSearchMatches> {
let mut matches = HashMap::new();
let mut dirs = vec![root.to_path_buf()];
+ let mut patterns = None;
while let Some(dir) = dirs.pop() {
let mut entries = match tokio::fs::read_dir(dir).await {
@@ -231,7 +249,7 @@ async fn scan_compressed_rollout_matches(
continue;
}
if let Some(snippet) =
- first_rollout_content_match_snippet(rollout_file.path(), search_term)
+ first_content_match_snippet(rollout_file.path(), search_term, &mut patterns)
.await
.unwrap_or_else(|err| {
tracing::warn!(
@@ -371,3 +389,7 @@ fn char_end_after(text: &str, byte_index: usize, chars_after: usize) -> usize {
.map(|(offset, _)| byte_index.saturating_add(offset))
.unwrap_or(text.len())
}
+
+#[cfg(test)]
+#[path = "search_tests.rs"]
+mod tests;
diff --git a/codex-rs/rollout/src/search_tests.rs b/codex-rs/rollout/src/search_tests.rs
new file mode 100644
index 0000000000..c9925e55e9
--- /dev/null
+++ b/codex-rs/rollout/src/search_tests.rs
@@ -0,0 +1,27 @@
+//! Regression coverage for rollout search error handling.
+
+use super::*;
+use pretty_assertions::assert_eq;
+use tempfile::TempDir;
+
+#[tokio::test]
+async fn compressed_search_preserves_error_handling() -> io::Result<()> {
+ let root = TempDir::new()?;
+ let path = root.path().join("rollout-0001.jsonl.zst");
+ std::fs::write(&path, zstd::stream::encode_all(&b""[..], 3)?)?;
+ let query = "a".repeat(500_000);
+
+ assert!(case_insensitive_literal_regex(&query).is_err());
+ assert_eq!(
+ scan_compressed_rollout_matches(root.path(), &query).await?,
+ RolloutSearchMatches::new()
+ );
+ assert_eq!(
+ first_rollout_content_match_snippet(&root.path().join("missing.jsonl"), &query)
+ .await
+ .unwrap_err()
+ .kind(),
+ io::ErrorKind::NotFound
+ );
+ Ok(())
+}