Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

feat(grep): 支持grep匹配行上下文 (#5168)

hezhang-1216a9ba33d0f1de
diff --git a/crates/ov_cli/src/client.rs b/crates/ov_cli/src/client.rs
index b53e0c492..51b3833b0 100644
--- a/crates/ov_cli/src/client.rs
+++ b/crates/ov_cli/src/client.rs
@@ -777,6 +777,8 @@ impl HttpClient {
exclude_uri: Option<String>,
pattern: &str,
ignore_case: bool,
+ after_context: i32,
+ before_context: i32,
node_limit: i32,
level_limit: i32,
tags: &[String],
@@ -787,6 +789,8 @@ impl HttpClient {
"exclude_uri": exclude_uri,
"pattern": pattern,
"case_insensitive": ignore_case,
+ "after_context": (after_context > 0).then_some(after_context),
+ "before_context": (before_context > 0).then_some(before_context),
"node_limit": node_limit,
"level_limit": level_limit,
"tags": (!tags.is_empty()).then(|| tags),
@@ -2504,6 +2508,8 @@ mod tests {
None,
"needle",
false,
+ 0,
+ 0,
10,
3,
&[],
@@ -2516,6 +2522,8 @@ mod tests {
assert!(request.starts_with("POST /api/v1/search/grep "));
assert!(!request.contains(r#""tags""#));
assert!(!request.contains(r#""include_tags""#));
+ assert!(!request.contains(r#""after_context""#));
+ assert!(!request.contains(r#""before_context""#));
}
#[tokio::test]
@@ -2529,6 +2537,8 @@ mod tests {
None,
"needle",
false,
+ 2,
+ 3,
10,
3,
&[],
@@ -2539,6 +2549,8 @@ mod tests {
let request = request_rx.await.expect("request should be captured");
assert!(request.contains(r#""include_tags":true"#));
+ assert!(request.contains(r#""after_context":2"#));
+ assert!(request.contains(r#""before_context":3"#));
}
#[tokio::test]
diff --git a/crates/ov_cli/src/commands/search.rs b/crates/ov_cli/src/commands/search.rs
index 99c670a92..92ef83a96 100644
--- a/crates/ov_cli/src/commands/search.rs
+++ b/crates/ov_cli/src/commands/search.rs
@@ -643,6 +643,8 @@ pub async fn grep(
exclude_uri: Option<String>,
pattern: &str,
ignore_case: bool,
+ after_context: i32,
+ before_context: i32,
node_limit: i32,
level_limit: i32,
tags: &[String],
@@ -656,6 +658,8 @@ pub async fn grep(
exclude_uri,
pattern,
ignore_case,
+ after_context,
+ before_context,
node_limit,
level_limit,
tags,
@@ -766,7 +770,14 @@ fn render_grep_match_card(
}
}
- if let Some(content) = object
+ let has_context = object.is_some_and(|object| {
+ object.contains_key("before_context") || object.contains_key("after_context")
+ });
+ if has_context {
+ render_grep_context_lines(object, "before_context", '-', text_width, lines);
+ render_grep_result_line(object, ':', text_width, true, lines);
+ render_grep_context_lines(object, "after_context", '-', text_width, lines);
+ } else if let Some(content) = object
.and_then(|object| object.get("content"))
.and_then(Value::as_str)
.map(str::trim)
@@ -796,6 +807,48 @@ fn render_grep_match_card(
}
}
+fn render_grep_context_lines(
+ object: Option<&serde_json::Map<String, Value>>,
+ key: &str,
+ separator: char,
+ text_width: usize,
+ lines: &mut Vec<String>,
+) {
+ if let Some(context) = object
+ .and_then(|object| object.get(key))
+ .and_then(Value::as_array)
+ {
+ for item in context {
+ render_grep_result_line(item.as_object(), separator, text_width, false, lines);
+ }
+ }
+}
+
+fn render_grep_result_line(
+ object: Option<&serde_json::Map<String, Value>>,
+ separator: char,
+ text_width: usize,
+ is_match: bool,
+ lines: &mut Vec<String>,
+) {
+ let Some(object) = object else {
+ return;
+ };
+ let Some(line_number) = object.get("line").and_then(Value::as_i64) else {
+ return;
+ };
+ let content = object.get("content").and_then(Value::as_str).unwrap_or("");
+ let text = format!("{line_number}{separator}{content}");
+ for line in wrap_display_text(&text, text_width, SEARCH_MAX_ABSTRACT_LINES) {
+ let styled = if is_match {
+ theme::body(line)
+ } else {
+ theme::muted(line)
+ };
+ lines.push(format!("{SEARCH_INDENT}{styled}"));
+ }
+}
+
fn pluralize<'a>(count: u64, singular: &'a str, plural: &'a str) -> &'a str {
if count == 1 { singular } else { plural }
}
@@ -1204,6 +1257,33 @@ mod tests {
assert!(rendered.contains("tags: env=prod, team=search"));
}
+ #[test]
+ fn grep_table_output_distinguishes_matches_from_context() {
+ let result = json!({
+ "matches": [{
+ "line": 3,
+ "uri": "viking://resources/a.md",
+ "content": "needle",
+ "before_context": [
+ {"line": 1, "content": "line one"},
+ {"line": 2, "content": "line two"}
+ ],
+ "after_context": [
+ {"line": 4, "content": "line four"}
+ ]
+ }],
+ "count": 1
+ });
+
+ let rendered =
+ strip_ansi(&render_grep_output_for_table(&result, OutputFormat::Table).expect("grep"));
+
+ assert!(rendered.contains("1-line one"));
+ assert!(rendered.contains("2-line two"));
+ assert!(rendered.contains("3:needle"));
+ assert!(rendered.contains("4-line four"));
+ }
+
#[test]
fn grep_table_output_handles_empty_matches() {
let result = json!({
diff --git a/crates/ov_cli/src/handlers.rs b/crates/ov_cli/src/handlers.rs
index 033db6ff6..5c00b2992 100644
--- a/crates/ov_cli/src/handlers.rs
+++ b/crates/ov_cli/src/handlers.rs
@@ -1903,6 +1903,8 @@ pub async fn handle_grep(
exclude_uri: Option<String>,
pattern: String,
ignore_case: bool,
+ after_context: i32,
+ before_context: i32,
node_limit: i32,
level_limit: i32,
tags: Vec<String>,
@@ -1927,6 +1929,12 @@ pub async fn handle_grep(
if ignore_case {
params.push("-i".to_string());
}
+ if after_context > 0 {
+ params.push(format!("-a {}", after_context));
+ }
+ if before_context > 0 {
+ params.push(format!("-b {}", before_context));
+ }
if !tags.is_empty() {
params.push(format!("--tags {}", tags.join(",")));
}
@@ -1942,6 +1950,8 @@ pub async fn handle_grep(
exclude_uri,
&pattern,
ignore_case,
+ after_context,
+ before_context,
node_limit,
level_limit,
&tags,
diff --git a/crates/ov_cli/src/main.rs b/crates/ov_cli/src/main.rs
index dc3e637cc..932185be6 100644
--- a/crates/ov_cli/src/main.rs
+++ b/crates/ov_cli/src/main.rs
@@ -940,6 +940,26 @@ enum Commands {
/// Case insensitive
#[arg(short, long, help_heading = "Common options")]
ignore_case: bool,
+ /// Number of lines to show after each match
+ #[arg(
+ short = 'a',
+ long = "after-context",
+ default_value = "0",
+ value_parser = clap::value_parser!(i32).range(0..),
+ value_name = "n",
+ help_heading = "Common options"
+ )]
+ after_context: i32,
+ /// Number of lines to show before each match
+ #[arg(
+ short = 'b',
+ long = "before-context",
+ default_value = "0",
+ value_parser = clap::value_parser!(i32).range(0..),
+ value_name = "n",
+ help_heading = "Common options"
+ )]
+ before_context: i32,
/// Maximum number of results
#[arg(
short = 'n',
@@ -3783,6 +3803,8 @@ async fn main() {
exclude_uri,
pattern,
ignore_case,
+ after_context,
+ before_context,
node_limit,
level_limit,
tags,
@@ -3793,6 +3815,8 @@ async fn main() {
exclude_uri,
pattern,
ignore_case,
+ after_context,
+ before_context,
node_limit,
level_limit,
tags,
@@ -4027,6 +4051,34 @@ mod tests {
}
}
+ #[test]
+ fn cli_parses_grep_context_lines() {
+ let cli = Cli::try_parse_from([
+ "ov",
+ "grep",
+ "--uri",
+ "viking://resources",
+ "-a",
+ "2",
+ "-b",
+ "3",
+ "needle",
+ ])
+ .expect("grep context should parse");
+
+ match cli.command {
+ Commands::Grep {
+ after_context,
+ before_context,
+ ..
+ } => {
+ assert_eq!(after_context, 2);
+ assert_eq!(before_context, 3);
+ }
+ _ => panic!("expected grep command"),
+ }
+ }
+
#[test]
fn cli_find_and_search_reject_removed_peer_id_flag() {
assert!(Cli::try_parse_from(["ov", "find", "invoice", "--peer-id", "peer-a"]).is_err());
diff --git a/crates/ragfs-python/src/lib.rs b/crates/ragfs-python/src/lib.rs
index 251038250..be31d2a22 100644
--- a/crates/ragfs-python/src/lib.rs
+++ b/crates/ragfs-python/src/lib.rs
@@ -204,8 +204,9 @@ use ragfs::core::builder::{
};
use ragfs::core::{
build_configured_stack, ConfigValue, FileInfo, FileSystem, FilesystemStats, FsContext,
- FsContextInner, FsOperation, GlobPage, GrepResult, ListSortBy, MountableFS, OperationStats,
- PathLockContext, PluginConfig, RagfsConfig, SortOrder, TreeEntry, WriteFlag, FS_CTX,
+ FsContextInner, FsOperation, GlobPage, GrepOptions, GrepResult, ListSortBy, MountableFS,
+ OperationStats, PathLockContext, PluginConfig, RagfsConfig, SortOrder, TreeEntry, WriteFlag,
+ FS_CTX,
};
use ragfs::lock::types::PathLockError;
use ragfs::lock::{
@@ -1057,7 +1058,7 @@ fn serde_json_to_py(py: Python<'_>, val: &serde_json::Value) -> PyResult<Py<PyAn
}
/// Convert GrepResult to a Python dict matching the Go binding JSON format:
-/// {"matches": [{"file": str, "line": int, "content": str}, ...], "count": int}
+/// {"matches": [{"file": str, "line": int, "content": str, ...}, ...], "count": int}
fn grep_result_to_py_dict(py: Python<'_>, result: &GrepResult) -> PyResult<Py<PyDict>> {
let dict = PyDict::new(py);
@@ -1067,6 +1068,26 @@ fn grep_result_to_py_dict(py: Python<'_>, result: &GrepResult) -> PyResult<Py<Py
match_dict.set_item("file", &m.file)?;
match_dict.set_item("line", m.line)?;
match_dict.set_item("content", &m.content)?;
+ if let Some(lines) = &m.before_context {
+ let context = PyList::empty(py);
+ for line in lines {
+ let item = PyDict::new(py);
+ item.set_item("line", line.line)?;
+ item.set_item("content", &line.content)?;
+ context.append(item)?;
+ }
+ match_dict.set_item("before_context", context)?;
+ }
+ if let Some(lines) = &m.after_context {
+ let context = PyList::empty(py);
+ for line in lines {
+ let item = PyDict::new(py);
+ item.set_item("line", line.line)?;
+ item.set_item("content", &line.content)?;
+ context.append(item)?;
+ }
+ match_dict.set_item("after_context", context)?;
+ }
matches_list.append(match_dict)?;
}
@@ -2241,10 +2262,12 @@ impl RAGFSBindingClient {
/// exclude_path: Optional path prefix to exclude from search (default: None)
/// level_limit: Optional maximum depth relative to query root (default: None)
/// ctx: Optional FsContext dict (e.g. {"account_id": ...})
+ /// before_context: Number of lines to include before each match (default: 0)
+ /// after_context: Number of lines to include after each match (default: 0)
///
/// Returns:
/// A dict with "matches" (list of match dicts) and "count" (total matches)
- #[pyo3(signature = (path, pattern, recursive=false, case_insensitive=false, stream=false, node_limit=None, exclude_path=None, level_limit=None, ctx=None))]
+ #[pyo3(signature = (path, pattern, recursive=false, case_insensitive=false, stream=false, node_limit=None, exclude_path=None, level_limit=None, ctx=None, before_context=0, after_context=0))]
#[allow(clippy::too_many_arguments)]
fn grep(
&self,
@@ -2258,6 +2281,8 @@ impl RAGFSBindingClient {
exclude_path: Option<String>,
level_limit: Option<i32>,
ctx: Option<HashMap<String, String>>,
+ before_context: i32,
+ after_context: i32,
) -> PyResult<Py<PyAny>> {
if stream {
return Err(PyRuntimeError::new_err(
@@ -2269,17 +2294,23 @@ impl RAGFSBindingClient {
let top = self.top.clone();
let limit = node_limit.map(|n| if n < 0 { 0 } else { n as usize });
let level_limit_usize = level_limit.map(|n| if n < 0 { 0 } else { n as usize });
+ let before_context = before_context.max(0) as usize;
+ let after_context = after_context.max(0) as usize;
let result = self
.run_scoped(py, fs_ctx, move || async move {
top.grep(
&path,
&pattern,
- recursive,
- case_insensitive,
- limit,
- exclude_path.as_deref(),
- level_limit_usize,
+ GrepOptions {
+ recursive,
+ case_insensitive,
+ node_limit: limit,
+ exclude_path: exclude_path.as_deref(),
+ level_limit: level_limit_usize,
+ before_context,
+ after_context,
+ },
)
.await
})
diff --git a/crates/ragfs/src/cache/wrapper.rs b/crates/ragfs/src/cache/wrapper.rs
index e2e62e248..15f92755a 100644
--- a/crates/ragfs/src/cache/wrapper.rs
+++ b/crates/ragfs/src/cache/wrapper.rs
@@ -7,9 +7,11 @@ use crate::core::filesystem::{
apply_read_dir_options, compile_grep_regex, is_excluded_path, normalize_prefix_path,
paginate_entries, relative_depth, relative_match_file,
};
+use crate::core::grep::GrepLineCollector;
+use crate::core::types::GrepContextLine;
use crate::core::{
- FileInfo, FileSystem, GlobPage, GrepMatch, GrepResult, ListSortBy, MultiWriteWrappedFS, Result,
- SortOrder, TreeEntry, WriteFlag,
+ FileInfo, FileSystem, GlobPage, GrepMatch, GrepOptions, GrepResult, ListSortBy,
+ MultiWriteWrappedFS, Result, SortOrder, TreeEntry, WriteFlag,
};
use async_trait::async_trait;
use bytes::Bytes;
@@ -192,19 +194,19 @@ impl CachedFileSystem {
&self,
path: &str,
pattern: &str,
- recursive: bool,
- case_insensitive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> Result<GrepResult> {
enum GrepTask {
Visit { path: String, is_dir: Option<bool> },
}
- let re = compile_grep_regex(pattern, case_insensitive)?;
+ let re = compile_grep_regex(pattern, options.case_insensitive)?;
let base_path = normalize_prefix_path(path);
- let normalized_exclude = exclude_path.map(normalize_prefix_path);
+ let normalized_exclude = options.exclude_path.map(normalize_prefix_path);
+ let options = GrepOptions {
+ exclude_path: normalized_exclude.as_deref(),
+ ..options
+ };
let mut result = GrepResult::new();
let generation_cache = Mutex::new(HashMap::new());
let mut file_batch = Vec::new();
@@ -218,7 +220,10 @@ impl CachedFileSystem {
is_dir,
}) = stack.pop()
{
- if node_limit.is_some_and(|limit| result.count >= limit) {
+ if options
+ .node_limit
+ .is_some_and(|limit| result.count >= limit)
+ {
break;
}
@@ -237,20 +242,23 @@ impl CachedFileSystem {
&mut file_batch,
&base_path,
&re,
- node_limit,
+ options,
&mut result,
&generation_cache,
)
.await?;
- if node_limit.is_some_and(|limit| result.count >= limit) {
+ if options
+ .node_limit
+ .is_some_and(|limit| result.count >= limit)
+ {
break;
}
- if !recursive && current_path != base_path {
+ if !options.recursive && current_path != base_path {
continue;
}
- if let Some(limit) = level_limit {
+ if let Some(limit) = options.level_limit {
let rel = relative_match_file(&base_path, &current_path);
if relative_depth(&rel) >= limit {
continue;
@@ -272,7 +280,7 @@ impl CachedFileSystem {
});
}
} else {
- if let Some(limit) = level_limit {
+ if let Some(limit) = options.level_limit {
let rel = relative_match_file(&base_path, &current_path);
if relative_depth(&rel) > limit {
continue;
@@ -285,7 +293,7 @@ impl CachedFileSystem {
&mut file_batch,
&base_path,
&re,
- node_limit,
+ options,
&mut result,
&generation_cache,
)
@@ -298,7 +306,7 @@ impl CachedFileSystem {
&mut file_batch,
&base_path,
&re,
- node_limit,
+ options,
&mut result,
&generation_cache,
)
@@ -312,23 +320,36 @@ impl CachedFileSystem {
file_batch: &mut Vec<String>,
base_path: &str,
re: &Regex,
- node_limit: Option<usize>,
+ options: GrepOptions<'_>,
result: &mut GrepResult,
generation_cache: &Mutex<HashMap<String, u64>>,
) -> Result<()> {
- if file_batch.is_empty() || node_limit.is_some_and(|limit| result.count >= limit) {
+ if file_batch.is_empty()
+ || options
+ .node_limit
+ .is_some_and(|limit| result.count >= limit)
+ {
file_batch.clear();
return Ok(());
}
- let remaining_limit = node_limit
+ let remaining_limit = options
+ .node_limit
.map(|limit| limit.saturating_sub(result.count))
.unwrap_or(usize::MAX);
let files = std::mem::take(file_batch);
let mut indexed = stream::iter(files.into_iter().enumerate())
.map(|(index, path)| async move {
let matches = self
- .grep_cached_file(&path, base_path, re, remaining_limit, generation_cache)
+ .grep_cached_file(
+ &path,
+ base_path,
+ re,
+ remaining_limit,
+ options.before_context,
+ options.after_context,
+ generation_cache,
+ )
.await;
(index, matches)
})
@@ -339,7 +360,10 @@ impl CachedFileSystem {
indexed.sort_by_key(|(index, _)| *index);
for (_, matches) in indexed {
for item in matches? {
- if node_limit.is_some_and(|limit| result.count >= limit) {
+ if options
+ .node_limit
+ .is_some_and(|limit| result.count >= limit)
+ {
return Ok(());
}
result.matches.push(item);
@@ -356,6 +380,8 @@ impl CachedFileSystem {
base_path: &str,
re: &Regex,
remaining_limit: usize,
+ before_context: usize,
+ after_context: usize,
generation_cache: &Mutex<HashMap<String, u64>>,
) -> Result<Vec<GrepMatch>> {
let content = self
@@ -363,22 +389,34 @@ impl CachedFileSystem {
.await?;
let content_str = String::from_utf8_lossy(&content);
let rel_file = relative_match_file(base_path, path);
- let mut matches = Vec::new();
-
- for (line_num, line) in content_str.lines().enumerate() {
- if matches.len() >= remaining_limit {
- break;
- }
- if re.is_match(line) {
- matches.push(GrepMatch {
- file: rel_file.clone(),
- line: (line_num + 1) as u64,
+ let mut result = GrepResult {
+ matches: Vec::with_capacity(remaining_limit.min(64)),
+ count: 0,
+ };
+ let mut collector = GrepLineCollector::new(
+ rel_file.clone(),
+ before_context,
+ after_context,
+ remaining_limit,
+ &mut result,
+ );
+
+ for (line_index, line) in content_str.lines().enumerate() {
+ let is_match = re.is_match(line);
+ if !collector.consume_line(
+ &rel_file,
+ GrepContextLine {
+ line: (line_index + 1) as u64,
content: line.to_string(),
- });
+ },
+ is_match,
+ ) {
+ break;
}
}
- Ok(matches)
+ drop(collector);
+ Ok(result.matches)
}
async fn cache_get(&self, key: &str) -> CacheResult<Option<Bytes>> {
@@ -1211,39 +1249,15 @@ impl FileSystem for CachedFileSystem {
&self,
path: &str,
pattern: &str,
- recursive: bool,
- case_insensitive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> Result<GrepResult> {
if self.policy.traversal_mode() == CacheTraversalMode::CachedTraversal
&& !self.wraps_multiwrite()
{
- return self
- .grep_via_cache(
- path,
- pattern,
- recursive,
- case_insensitive,
- node_limit,
- exclude_path,
- level_limit,
- )
- .await;
+ return self.grep_via_cache(path, pattern, options).await;
}
- self.backend
- .grep(
- path,
- pattern,
- recursive,
- case_insensitive,
- node_limit,
- exclude_path,
- level_limit,
- )
- .await
+ self.backend.grep(path, pattern, options).await
}
async fn tree_directory(
diff --git a/crates/ragfs/src/core/encryption_wrapper.rs b/crates/ragfs/src/core/encryption_wrapper.rs
index ce26b3905..904521097 100644
--- a/crates/ragfs/src/core/encryption_wrapper.rs
+++ b/crates/ragfs/src/core/encryption_wrapper.rs
@@ -26,7 +26,9 @@ use super::errors::{Error, Result};
use super::filesystem::{
apply_read_dir_options, compile_grep_regex, normalize_prefix_path, paginate_entries, FileSystem,
};
-use super::types::{FileInfo, GlobPage, GrepResult, ListSortBy, SortOrder, TreeEntry, WriteFlag};
+use super::types::{
+ FileInfo, GlobPage, GrepOptions, GrepResult, ListSortBy, SortOrder, TreeEntry, WriteFlag,
+};
const SYSTEM_ACCOUNT_ID: &str = "_system";
const TEMP_ROOT_CACHE_TTL: Duration = Duration::from_secs(15 * 60);
@@ -444,45 +446,25 @@ impl FileSystem for EncryptionWrappedFS {
&self,
path: &str,
pattern: &str,
- recursive: bool,
- case_insensitive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> Result<GrepResult> {
if Self::should_passthrough_content(path) {
- return self
- .inner
- .grep(
- path,
- pattern,
- recursive,
- case_insensitive,
- node_limit,
- exclude_path,
- level_limit,
- )
- .await;
+ return self.inner.grep(path, pattern, options).await;
}
// This layer only exists when encryption is on, so always traverse via the trait default
// grep_internal -> grep_file -> self.read (which decrypts). Never delegate to inner.grep:
// the plugin override would run the regex against raw ciphertext and miss every file.
- let re = compile_grep_regex(pattern, case_insensitive)?;
+ let re = compile_grep_regex(pattern, options.case_insensitive)?;
let base = normalize_prefix_path(path);
- let excl = exclude_path.map(normalize_prefix_path);
+ let excl = options.exclude_path.map(normalize_prefix_path);
+ let options = GrepOptions {
+ exclude_path: excl.as_deref(),
+ ..options
+ };
let mut result = GrepResult::new();
- self.grep_internal(
- &base,
- &base,
- &re,
- recursive,
- node_limit,
- excl.as_deref(),
- level_limit,
- &mut result,
- )
- .await?;
+ self.grep_internal(&base, &base, &re, options, &mut result)
+ .await?;
Ok(result)
}
@@ -770,7 +752,14 @@ mod tests {
.await
.unwrap();
let res = enc
- .grep("/mem", "NEEDLE", true, false, None, None, None)
+ .grep(
+ "/mem",
+ "NEEDLE",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
assert_eq!(res.count, 1);
diff --git a/crates/ragfs/src/core/filesystem.rs b/crates/ragfs/src/core/filesystem.rs
index 429341f0b..8ce18bbb0 100644
--- a/crates/ragfs/src/core/filesystem.rs
+++ b/crates/ragfs/src/core/filesystem.rs
@@ -13,7 +13,8 @@ use std::path::{Component, Path};
use super::errors::{Error, Result};
use super::glob::{compare_rel_paths, decode_offset_token, encode_offset_token, PreparedGlob};
use super::types::{
- FileInfo, GlobEntry, GlobPage, GrepResult, ListSortBy, SortOrder, TreeEntry, WriteFlag,
+ FileInfo, GlobEntry, GlobPage, GrepMatch, GrepOptions, GrepResult, ListSortBy, SortOrder,
+ TreeEntry, WriteFlag,
};
/// Reject virtual paths that can escape a mounted backend lexically.
@@ -427,11 +428,7 @@ pub trait FileSystem: Send + Sync + Any {
/// # Arguments
/// * `path` - The path to search (file or directory)
/// * `pattern` - The regular expression pattern to search for
- /// * `recursive` - Whether to search recursively in subdirectories
- /// * `case_insensitive` - Whether to perform case-insensitive matching
- /// * `node_limit` - Maximum number of matches to return (None means no limit)
- /// * `exclude_path` - Optional path prefix to exclude from search
- /// * `level_limit` - Optional maximum depth relative to query root
+ /// * `options` - Search behavior and result limits
///
/// # Returns
/// A GrepResult containing all matches found
@@ -443,26 +440,23 @@ pub trait FileSystem: Send + Sync + Any {
&self,
path: &str,
pattern: &str,
- recursive: bool,
- case_insensitive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> Result<GrepResult> {
- let re = compile_grep_regex(pattern, case_insensitive)?;
+ let re = compile_grep_regex(pattern, options.case_insensitive)?;
let mut result = GrepResult::new();
let normalized_path = normalize_prefix_path(path);
- let normalized_exclude = exclude_path.map(normalize_prefix_path);
+ let normalized_exclude = options.exclude_path.map(normalize_prefix_path);
+ let normalized_options = GrepOptions {
+ exclude_path: normalized_exclude.as_deref(),
+ ..options
+ };
self.grep_internal(
normalized_path.as_str(),
normalized_path.as_str(),
&re,
- recursive,
- node_limit,
- normalized_exclude.as_deref(),
- level_limit,
+ normalized_options,
&mut result,
)
.await?;
@@ -476,17 +470,17 @@ pub trait FileSystem: Send + Sync + Any {
base_path: &str,
current_path: &str,
re: &Regex,
- recursive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
result: &mut GrepResult,
) -> Result<()> {
- if node_limit.is_some_and(|limit| result.count >= limit) {
+ if options
+ .node_limit
+ .is_some_and(|limit| result.count >= limit)
+ {
return Ok(());
}
- if let Some(exclude) = exclude_path {
+ if let Some(exclude) = options.exclude_path {
if is_excluded_path(current_path, exclude) {
return Ok(());
}
@@ -495,11 +489,11 @@ pub trait FileSystem: Send + Sync + Any {
let stat = self.stat(current_path).await?;
if stat.is_dir {
- if !recursive && current_path != base_path {
+ if !options.recursive && current_path != base_path {
return Ok(());
}
- if let Some(limit) = level_limit {
+ if let Some(limit) = options.level_limit {
let rel = relative_match_file(base_path, current_path);
let depth = relative_depth(&rel);
// Directories at depth >= limit cannot contain files within the limit.
@@ -511,7 +505,10 @@ pub trait FileSystem: Send + Sync + Any {
let entries = self.read_dir(current_path, None, None, None, None).await?;
for entry in entries {
- if node_limit.is_some_and(|limit| result.count >= limit) {
+ if options
+ .node_limit
+ .is_some_and(|limit| result.count >= limit)
+ {
break;
}
@@ -521,20 +518,11 @@ pub trait FileSystem: Send + Sync + Any {
format!("{}/{}", current_path, entry.name)
};
- self.grep_internal(
- base_path,
- &entry_path,
- re,
- recursive,
- node_limit,
- exclude_path,
- level_limit,
- result,
- )
- .await?;
+ self.grep_internal(base_path, &entry_path, re, options, result)
+ .await?;
}
} else {
- if let Some(limit) = level_limit {
+ if let Some(limit) = options.level_limit {
let rel = relative_match_file(base_path, current_path);
let depth = relative_depth(&rel);
if depth > limit {
@@ -542,7 +530,7 @@ pub trait FileSystem: Send + Sync + Any {
}
}
- self.grep_file(base_path, current_path, re, node_limit, result)
+ self.grep_file(base_path, current_path, re, options, result)
.await?;
}
@@ -555,26 +543,39 @@ pub trait FileSystem: Send + Sync + Any {
base_path: &str,
path: &str,
re: &Regex,
- node_limit: Option<usize>,
+ options: GrepOptions<'_>,
result: &mut GrepResult,
) -> Result<()> {
- if node_limit.is_some_and(|limit| result.count >= limit) {
+ if options
+ .node_limit
+ .is_some_and(|limit| result.count >= limit)
+ {
return Ok(());
}
let content = self.read(path, 0, 0).await?;
let content_str = String::from_utf8_lossy(&content);
-
let rel_file = relative_match_file(base_path, path);
+ let lines: Vec<_> = content_str.lines().collect();
- for (line_num, line) in content_str.lines().enumerate() {
- if node_limit.is_some_and(|limit| result.count >= limit) {
+ for (line_index, line) in lines.iter().enumerate() {
+ if options
+ .node_limit
+ .is_some_and(|limit| result.count >= limit)
+ {
break;
}
if re.is_match(line) {
- result.add_match(rel_file.clone(), (line_num + 1) as u64, line.to_string());
+ result.matches.push(GrepMatch::from_lines(
+ rel_file.clone(),
+ &lines,
+ line_index,
+ options.before_context,
+ options.after_context,
+ ));
+ result.count += 1;
}
}
@@ -991,7 +992,14 @@ mod tests {
.with_file("/root/sub/b.txt", "hello\n");
let out = fs
- .grep("/root", "hello", true, false, None, None, None)
+ .grep(
+ "/root",
+ "hello",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
@@ -1000,6 +1008,39 @@ mod tests {
assert!(files.contains(&"sub/b.txt".to_string()));
}
+ #[tokio::test]
+ async fn test_default_grep_includes_bounded_context() {
+ let fs = TreeFS::default().with_file("/root/a.txt", "first\nbefore\nhit\nafter\n");
+
+ let out = fs
+ .grep(
+ "/root/a.txt",
+ "hit",
+ GrepOptions {
+ recursive: false,
+ before_context: 2,
+ after_context: 2,
+ ..Default::default()
+ },
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(out.count, 1);
+ let matched = &out.matches[0];
+ assert_eq!(matched.line, 3);
+ let before = matched.before_context.as_ref().unwrap();
+ assert_eq!(before.len(), 2);
+ assert_eq!(before[0].line, 1);
+ assert_eq!(before[0].content, "first");
+ assert_eq!(before[1].line, 2);
+ assert_eq!(before[1].content, "before");
+ let after = matched.after_context.as_ref().unwrap();
+ assert_eq!(after.len(), 1);
+ assert_eq!(after[0].line, 4);
+ assert_eq!(after[0].content, "after");
+ }
+
#[tokio::test]
async fn test_default_grep_exclude_path_applies_before_node_limit() {
let fs = TreeFS::default()
@@ -1013,11 +1054,12 @@ mod tests {
.grep(
"/root",
"hit",
- true,
- false,
- Some(1),
- Some("/root/excluded"),
- None,
+ GrepOptions {
+ recursive: true,
+ node_limit: Some(1),
+ exclude_path: Some("/root/excluded"),
+ ..Default::default()
+ },
)
.await
.unwrap();
@@ -1035,7 +1077,16 @@ mod tests {
.with_file("/root/deep/b.txt", "hit\n");
let out = fs
- .grep("/root", "hit", true, false, Some(1), None, Some(1))
+ .grep(
+ "/root",
+ "hit",
+ GrepOptions {
+ recursive: true,
+ node_limit: Some(1),
+ level_limit: Some(1),
+ ..Default::default()
+ },
+ )
.await
.unwrap();
diff --git a/crates/ragfs/src/core/grep.rs b/crates/ragfs/src/core/grep.rs
new file mode 100644
index 000000000..441f21d90
--- /dev/null
+++ b/crates/ragfs/src/core/grep.rs
@@ -0,0 +1,109 @@
+use std::collections::VecDeque;
+
+use super::types::{GrepContextLine, GrepMatch, GrepResult};
+
+/// Collects ordered line events into grep matches with optional context.
+pub(crate) struct GrepLineCollector<'a> {
+ file: String,
+ last_line: Option<u64>,
+ before_context: usize,
+ after_context: usize,
+ limit: usize,
+ history: VecDeque<GrepContextLine>,
+ pending_after: VecDeque<usize>,
+ result: &'a mut GrepResult,
+}
+
+impl<'a> GrepLineCollector<'a> {
+ pub(crate) fn new(
+ file: String,
+ before_context: usize,
+ after_context: usize,
+ limit: usize,
+ result: &'a mut GrepResult,
+ ) -> Self {
+ Self {
+ file,
+ last_line: None,
+ before_context,
+ after_context,
+ limit,
+ history: VecDeque::with_capacity(before_context),
+ pending_after: VecDeque::new(),
+ result,
+ }
+ }
+
+ pub(crate) fn consume_line(
+ &mut self,
+ file: &str,
+ line: GrepContextLine,
+ is_match: bool,
+ ) -> bool {
+ if self.file != file
+ || self
+ .last_line
+ .is_some_and(|last_line| line.line != last_line + 1)
+ {
+ self.file.clear();
+ self.file.push_str(file);
+ self.break_context();
+ }
+ self.last_line = Some(line.line);
+
+ for &match_index in &self.pending_after {
+ self.result.matches[match_index]
+ .after_context
+ .as_mut()
+ .expect("after context is initialized when requested")
+ .push(line.clone());
+ }
+ while self.pending_after.front().is_some_and(|&match_index| {
+ self.result.matches[match_index]
+ .after_context
+ .as_ref()
+ .is_some_and(|context| context.len() >= self.after_context)
+ }) {
+ self.pending_after.pop_front();
+ }
+
+ if is_match && self.result.count < self.limit {
+ let match_index = self.result.matches.len();
+ self.result.matches.push(GrepMatch {
+ file: self.file.clone(),
+ line: line.line,
+ content: line.content.clone(),
+ before_context: (self.before_context > 0)
+ .then(|| self.history.iter().cloned().collect()),
+ after_context: (self.after_context > 0).then(Vec::new),
+ });
+ self.result.count += 1;
+ if self.after_context > 0 {
+ self.pending_after.push_back(match_index);
+ }
+ }
+
+ if self.before_context > 0 {
+ self.history.push_back(line);
+ if self.history.len() > self.before_context {
+ self.history.pop_front();
+ }
+ }
+
+ !self.limit_reached()
+ }
+
+ pub(crate) fn break_context(&mut self) {
+ self.history.clear();
+ self.pending_after.clear();
+ self.last_line = None;
+ }
+
+ pub(crate) fn limit_reached(&self) -> bool {
+ self.result.count >= self.limit && self.pending_after.is_empty()
+ }
+
+ pub(crate) fn match_count(&self) -> usize {
+ self.result.count
+ }
+}
diff --git a/crates/ragfs/src/core/mod.rs b/crates/ragfs/src/core/mod.rs
index 632af6545..fb75b3d52 100644
--- a/crates/ragfs/src/core/mod.rs
+++ b/crates/ragfs/src/core/mod.rs
@@ -13,6 +13,7 @@ pub mod encryption_wrapper;
pub mod errors;
pub mod filesystem;
pub mod glob;
+pub(crate) mod grep;
pub mod internal_names;
pub mod mountable;
pub mod multibackend_wrapper;
@@ -42,7 +43,7 @@ pub use stats::{FilesystemStats, FsOperation, OperationStats, OperationTimer, St
pub use stats_wrapper::StatsWrappedFS;
pub use types::{
BackendItemConfig, BackendRole, BackendSyncState, BackendsConfig, ConfigParameter, ConfigValue,
- EncryptionConfig, FileInfo, GlobEntry, GlobPage, GrepMatch, GrepResult, ListSortBy,
- OperationItemConfig, PluginConfig, RedirectEntry, RedirectMeta, RedirectPolicy, SortOrder,
- SyncLogEntry, SyncLogMeta, SyncOp, SyncType, TreeEntry, WriteFlag,
+ EncryptionConfig, FileInfo, GlobEntry, GlobPage, GrepMatch, GrepOptions, GrepResult,
+ ListSortBy, OperationItemConfig, PluginConfig, RedirectEntry, RedirectMeta, RedirectPolicy,
+ SortOrder, SyncLogEntry, SyncLogMeta, SyncOp, SyncType, TreeEntry, WriteFlag,
};
diff --git a/crates/ragfs/src/core/mountable.rs b/crates/ragfs/src/core/mountable.rs
index 995604558..4e46d42af 100644
--- a/crates/ragfs/src/core/mountable.rs
+++ b/crates/ragfs/src/core/mountable.rs
@@ -35,7 +35,7 @@ use super::plugin::ServicePlugin;
use super::stats::{FilesystemStats, StatsCollector};
use super::stats_wrapper::StatsWrappedFS;
use super::types::{
- BackendsConfig, FileInfo, GlobPage, GrepResult, PluginConfig, TreeEntry, WriteFlag,
+ BackendsConfig, FileInfo, GlobPage, GrepOptions, GrepResult, PluginConfig, TreeEntry, WriteFlag,
};
#[cfg(feature = "cache")]
use crate::cache::{CacheNamespace, CachePolicy, CacheTraversalMode, CachedFileSystem};
@@ -1095,11 +1095,7 @@ impl FileSystem for MountableFS {
&self,
path: &str,
pattern: &str,
- recursive: bool,
- case_insensitive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> Result<GrepResult> {
// Route grep to the mounted plugin so plugin-specific fast paths (e.g. localfs + rg)
// can take effect. If a plugin doesn't override grep, it will fall back to the trait
@@ -1108,7 +1104,7 @@ impl FileSystem for MountableFS {
// Exclude path only applies when it resolves to the same mount point; otherwise it
// should not affect searching under `path`.
- let exclude_rel: Option<String> = match exclude_path {
+ let exclude_rel: Option<String> = match options.exclude_path {
None => None,
Some(excl_abs) => match self.find_mount(excl_abs).await {
Ok((exclude_mount, excl_rel)) => {
@@ -1127,11 +1123,10 @@ impl FileSystem for MountableFS {
.grep(
&rel_path,
pattern,
- recursive,
- case_insensitive,
- node_limit,
- exclude_rel.as_deref(),
- level_limit,
+ GrepOptions {
+ exclude_path: exclude_rel.as_deref(),
+ ..options
+ },
)
.await?;
@@ -1391,11 +1386,7 @@ mod tests {
&self,
path: &str,
pattern: &str,
- _recursive: bool,
- _case_insensitive: bool,
- _node_limit: Option<usize>,
- _exclude_path: Option<&str>,
- _level_limit: Option<usize>,
+ _options: GrepOptions<'_>,
) -> Result<GrepResult> {
let mut out = GrepResult::new();
// Encode the received rel_path into the match so the test can assert routing worked.
@@ -2214,7 +2205,7 @@ mod tests {
let mfs = mounted_mock("mock", "/mock").await;
let result = mfs
- .grep("/mock/a.txt", "foo", false, false, None, None, None)
+ .grep("/mock/a.txt", "foo", GrepOptions::default())
.await
.unwrap();
diff --git a/crates/ragfs/src/core/multibackend_wrapper.rs b/crates/ragfs/src/core/multibackend_wrapper.rs
index 519dd9e1b..4edc19054 100644
--- a/crates/ragfs/src/core/multibackend_wrapper.rs
+++ b/crates/ragfs/src/core/multibackend_wrapper.rs
@@ -28,9 +28,9 @@ use super::filesystem::{
relative_match_file, FileSystem,
};
use super::types::{
- BackendRole, BackendSyncState, FileInfo, GlobEntry, GlobPage, GrepResult, ListSortBy,
- OperationItemConfig, RedirectEntry, RedirectPolicy, SortOrder, SyncLogEntry, SyncOp, SyncType,
- TreeEntry, WriteFlag,
+ BackendRole, BackendSyncState, FileInfo, GlobEntry, GlobPage, GrepOptions, GrepResult,
+ ListSortBy, OperationItemConfig, RedirectEntry, RedirectPolicy, SortOrder, SyncLogEntry,
+ SyncOp, SyncType, TreeEntry, WriteFlag,
};
use crate::core::glob::{
compare_rel_paths, decode_offset_token, encode_offset_token, PreparedGlob,
@@ -1565,29 +1565,24 @@ impl FileSystem for MultiWriteWrappedFS {
&self,
path: &str,
pattern: &str,
- recursive: bool,
- case_insensitive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> Result<GrepResult> {
let inner = &self.inner;
let path_owned = path.to_string();
let pattern_owned = pattern.to_string();
- let exclude_owned = exclude_path.map(|s| s.to_string());
+ let exclude_owned = options.exclude_path.map(str::to_string);
+ let options = GrepOptions {
+ exclude_path: exclude_owned.as_deref(),
+ ..options
+ };
+ let recursive = options.recursive;
+ let node_limit = options.node_limit;
+ let level_limit = options.level_limit;
let mut result = inner
.primary()
.backend
- .grep(
- &path_owned,
- &pattern_owned,
- recursive,
- case_insensitive,
- node_limit,
- exclude_owned.as_deref(),
- level_limit,
- )
+ .grep(&path_owned, &pattern_owned, options)
.await?;
// Filter out multi-write internal metadata files from grep results.
@@ -1642,22 +1637,26 @@ impl FileSystem for MultiWriteWrappedFS {
.grep(
&entry.path,
&pattern_owned,
- false,
- case_insensitive,
- node_limit.map(|limit| limit.saturating_sub(result.count)),
- None,
- None,
+ GrepOptions {
+ recursive: false,
+ node_limit: node_limit.map(|limit| limit.saturating_sub(result.count)),
+ exclude_path: None,
+ level_limit: None,
+ ..options
+ },
)
.await
{
Ok(found) => found,
Err(_) => continue,
};
- for m in target_result.matches {
+ for mut m in target_result.matches {
if node_limit.is_some_and(|limit| result.count >= limit) {
break;
}
- result.add_match(rel_path.clone(), m.line, m.content);
+ m.file = rel_path.clone();
+ result.matches.push(m);
+ result.count += 1;
}
}
return Ok(result);
@@ -1677,20 +1676,23 @@ impl FileSystem for MultiWriteWrappedFS {
.grep(
&redirect_path,
&pattern_owned,
- false,
- case_insensitive,
- node_limit,
- None,
- None,
+ GrepOptions {
+ recursive: false,
+ exclude_path: None,
+ level_limit: None,
+ ..options
+ },
)
.await
{
let rel_path = relative_match_file(&path_owned, &redirect_path);
- for m in target_result.matches {
+ for mut m in target_result.matches {
if node_limit.is_some_and(|limit| result.count >= limit) {
break;
}
- result.add_match(rel_path.clone(), m.line, m.content);
+ m.file = rel_path.clone();
+ result.matches.push(m);
+ result.count += 1;
}
}
}
diff --git a/crates/ragfs/src/core/multibackend_wrapper/tests.rs b/crates/ragfs/src/core/multibackend_wrapper/tests.rs
index bc3763a26..1547e55e7 100644
--- a/crates/ragfs/src/core/multibackend_wrapper/tests.rs
+++ b/crates/ragfs/src/core/multibackend_wrapper/tests.rs
@@ -161,11 +161,10 @@ async fn test_read_dir_redirect_entries_use_target_stat() {
.grep(
"/local/acct/docs",
"GHOST_MARKER",
- false,
- false,
- None,
- None,
- Some(1),
+ GrepOptions {
+ level_limit: Some(1),
+ ..Default::default()
+ },
)
.await?;
assert_eq!(grep.count, 0);
@@ -270,11 +269,11 @@ async fn test_recursive_grep_finds_nested_redirected_files() {
.grep(
"/local/acct/resources",
"MULTIWRITE_TEST_MARKER",
- true,
- false,
- None,
- None,
- Some(5),
+ GrepOptions {
+ recursive: true,
+ level_limit: Some(5),
+ ..Default::default()
+ },
)
.await?;
assert_eq!(result.count, 1);
diff --git a/crates/ragfs/src/core/stats_wrapper.rs b/crates/ragfs/src/core/stats_wrapper.rs
index c40a54b2c..4235328ee 100644
--- a/crates/ragfs/src/core/stats_wrapper.rs
+++ b/crates/ragfs/src/core/stats_wrapper.rs
@@ -7,7 +7,7 @@ use async_trait::async_trait;
use std::sync::Arc;
use super::{
- FileInfo, FileSystem, FsOperation, GlobPage, GrepResult, OperationTimer, Result,
+ FileInfo, FileSystem, FsOperation, GlobPage, GrepOptions, GrepResult, OperationTimer, Result,
StatsCollector, TreeEntry, WriteFlag,
};
@@ -176,25 +176,10 @@ impl FileSystem for StatsWrappedFS {
&self,
path: &str,
pattern: &str,
- recursive: bool,
- case_insensitive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> Result<GrepResult> {
let timer = OperationTimer::start(FsOperation::Grep, Arc::clone(&self.stats));
- let result = self
- .inner
- .grep(
- path,
- pattern,
- recursive,
- case_insensitive,
- node_limit,
- exclude_path,
- level_limit,
- )
- .await;
+ let result = self.inner.grep(path, pattern, options).await;
timer.finish(result.is_ok()).await;
result
}
diff --git a/crates/ragfs/src/core/types.rs b/crates/ragfs/src/core/types.rs
index eb0af3c76..fff95a76a 100644
--- a/crates/ragfs/src/core/types.rs
+++ b/crates/ragfs/src/core/types.rs
@@ -28,6 +28,39 @@ pub enum SortOrder {
Desc,
}
+/// Options for a grep operation.
+#[derive(Debug, Clone, Copy)]
+pub struct GrepOptions<'a> {
+ /// Whether to search recursively in subdirectories.
+ pub recursive: bool,
+ /// Whether matching is case-insensitive.
+ pub case_insensitive: bool,
+ /// Maximum number of matches to return.
+ pub node_limit: Option<usize>,
+ /// Optional path prefix to exclude.
+ pub exclude_path: Option<&'a str>,
+ /// Optional maximum depth relative to the query root.
+ pub level_limit: Option<usize>,
+ /// Number of lines to include before each match.
+ pub before_context: usize,
+ /// Number of lines to include after each match.
+ pub after_context: usize,
+}
+
+impl Default for GrepOptions<'_> {
+ fn default() -> Self {
+ Self {
+ recursive: false,
+ case_insensitive: false,
+ node_limit: None,
+ exclude_path: None,
+ level_limit: None,
+ before_context: 0,
+ after_context: 0,
+ }
+ }
+}
+
/// Grep match result
///
/// Represents a single match found during a grep operation.
@@ -41,6 +74,62 @@ pub struct GrepMatch {
/// Content of the matched line
pub content: String,
+
+ /// Lines immediately before the match
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub before_context: Option<Vec<GrepContextLine>>,
+
+ /// Lines immediately after the match
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub after_context: Option<Vec<GrepContextLine>>,
+}
+
+/// One line included as grep context.
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct GrepContextLine {
+ /// Line number (1-based)
+ pub line: u64,
+
+ /// Line content
+ pub content: String,
+}
+
+impl GrepMatch {
+ /// Build a grep match and slice optional context from already-read lines.
+ pub fn from_lines(
+ file: String,
+ lines: &[&str],
+ line_index: usize,
+ before_context: usize,
+ after_context: usize,
+ ) -> Self {
+ let before = (before_context > 0).then(|| {
+ let start = line_index.saturating_sub(before_context);
+ (start..line_index)
+ .map(|index| GrepContextLine {
+ line: (index + 1) as u64,
+ content: lines[index].to_string(),
+ })
+ .collect()
+ });
+ let after = (after_context > 0).then(|| {
+ let end = lines.len().min(line_index + after_context + 1);
+ (line_index + 1..end)
+ .map(|index| GrepContextLine {
+ line: (index + 1) as u64,
+ content: lines[index].to_string(),
+ })
+ .collect()
+ });
+
+ Self {
+ file,
+ line: (line_index + 1) as u64,
+ content: lines[line_index].to_string(),
+ before_context: before,
+ after_context: after,
+ }
+ }
}
/// Grep operation result
@@ -130,6 +219,8 @@ impl GrepResult {
file,
line,
content,
+ before_context: None,
+ after_context: None,
});
self.count += 1;
}
diff --git a/crates/ragfs/src/lock/wrapper.rs b/crates/ragfs/src/lock/wrapper.rs
index ec6bdc7fd..d34db33e1 100644
--- a/crates/ragfs/src/lock/wrapper.rs
+++ b/crates/ragfs/src/lock/wrapper.rs
@@ -14,7 +14,7 @@ use tracing::debug;
use crate::core::filesystem::FileSystem;
use crate::core::internal_names::is_hidden_runtime_lock_name;
use crate::core::types::{
- FileInfo, GlobPage, GrepResult, ListSortBy, SortOrder, TreeEntry, WriteFlag,
+ FileInfo, GlobPage, GrepOptions, GrepResult, ListSortBy, SortOrder, TreeEntry, WriteFlag,
};
use crate::core::MountableFS;
@@ -339,23 +339,9 @@ impl FileSystem for PathLockWrappedFS {
&self,
path: &str,
pattern: &str,
- recursive: bool,
- case_insensitive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> crate::core::Result<GrepResult> {
- self.inner
- .grep(
- path,
- pattern,
- recursive,
- case_insensitive,
- node_limit,
- exclude_path,
- level_limit,
- )
- .await
+ self.inner.grep(path, pattern, options).await
}
async fn tree_directory(
diff --git a/crates/ragfs/src/plugins/localfs/mod.rs b/crates/ragfs/src/plugins/localfs/mod.rs
index f5bf90fa6..0283eeb37 100644
--- a/crates/ragfs/src/plugins/localfs/mod.rs
+++ b/crates/ragfs/src/plugins/localfs/mod.rs
@@ -7,23 +7,92 @@ use async_trait::async_trait;
use std::fs;
use std::path::{Path, PathBuf};
-use std::io::{BufRead, BufReader, ErrorKind};
+use std::io::{self, BufRead, BufReader, ErrorKind};
use std::process::{Command, Stdio};
use grep_regex::RegexMatcher;
-use grep_searcher::sinks::UTF8;
-use grep_searcher::{BinaryDetection, SearcherBuilder};
+use grep_searcher::{
+ BinaryDetection, Searcher, SearcherBuilder, Sink, SinkContext, SinkContextKind, SinkMatch,
+};
use ignore::WalkBuilder;
use serde::Deserialize;
use crate::core::errors::{Error, Result};
use crate::core::filesystem::{validate_virtual_path, FileSystem};
use crate::core::glob::{decode_offset_token, encode_offset_token, PreparedGlob};
+use crate::core::grep::GrepLineCollector;
use crate::core::plugin::ServicePlugin;
use crate::core::types::{
- ConfigParameter, FileInfo, GlobEntry, GlobPage, GrepResult, PluginConfig, WriteFlag,
+ ConfigParameter, FileInfo, GlobEntry, GlobPage, GrepContextLine, GrepOptions, GrepResult,
+ PluginConfig, WriteFlag,
};
+struct LocalGrepSink<'a> {
+ file: String,
+ collector: GrepLineCollector<'a>,
+}
+
+impl<'a> LocalGrepSink<'a> {
+ fn new(
+ file: String,
+ before_context: usize,
+ after_context: usize,
+ limit: usize,
+ result: &'a mut GrepResult,
+ ) -> Self {
+ Self {
+ collector: GrepLineCollector::new(
+ file.clone(),
+ before_context,
+ after_context,
+ limit,
+ result,
+ ),
+ file,
+ }
+ }
+
+ fn decode_line(line_number: Option<u64>, bytes: &[u8]) -> io::Result<GrepContextLine> {
+ let line_number = line_number.ok_or_else(|| {
+ io::Error::new(io::ErrorKind::InvalidData, "line numbers not enabled")
+ })?;
+ let content = std::str::from_utf8(bytes)
+ .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?
+ .trim_end_matches(&['\r', '\n'][..])
+ .to_string();
+ Ok(GrepContextLine {
+ line: line_number,
+ content,
+ })
+ }
+
+ fn consume_line(&mut self, file: &str, line: GrepContextLine, is_match: bool) -> bool {
+ self.collector.consume_line(file, line, is_match)
+ }
+}
+
+impl Sink for LocalGrepSink<'_> {
+ type Error = io::Error;
+
+ fn matched(&mut self, _searcher: &Searcher, matched: &SinkMatch<'_>) -> io::Result<bool> {
+ let line = Self::decode_line(matched.line_number(), matched.bytes())?;
+ Ok(self.collector.consume_line(&self.file, line, true))
+ }
+
+ fn context(&mut self, _searcher: &Searcher, context: &SinkContext<'_>) -> io::Result<bool> {
+ if context.kind() == &SinkContextKind::Other {
+ return Ok(true);
+ }
+ let line = Self::decode_line(context.line_number(), context.bytes())?;
+ Ok(self.collector.consume_line(&self.file, line, false))
+ }
+
+ fn context_break(&mut self, _searcher: &Searcher) -> io::Result<bool> {
+ self.collector.break_context();
+ Ok(!self.collector.limit_reached())
+ }
+}
+
/// LocalFS - Local file system implementation
pub struct LocalFileSystem {
/// Base path of the mounted directory
@@ -365,13 +434,9 @@ impl LocalFileSystem {
base_path: &Path,
target_path: &Path,
pattern: &str,
- recursive: bool,
- case_insensitive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> Result<Option<GrepResult>> {
- if node_limit == Some(0) {
+ if options.node_limit == Some(0) {
return Ok(Some(GrepResult::new()));
}
@@ -380,17 +445,25 @@ impl LocalFileSystem {
cmd.arg("--no-messages"); // Avoid interleaving permission/IO warnings with JSON output
cmd.arg("--no-ignore-parent"); // Match fallback `.parents(false)` semantics.
- if case_insensitive {
+ if options.case_insensitive {
cmd.arg("-i");
}
+ if options.before_context > 0 {
+ cmd.arg("--before-context")
+ .arg(options.before_context.to_string());
+ }
+ if options.after_context > 0 {
+ cmd.arg("--after-context")
+ .arg(options.after_context.to_string());
+ }
// Non-recursive directory search: only scan the current directory (no descent).
- if !recursive && target_path.is_dir() {
+ if !options.recursive && target_path.is_dir() {
cmd.arg("--max-depth").arg("1");
}
// NOTE: rg's --max-count is a per-file limit; we enforce a global limit by terminating early while parsing.
- if let Some(limit) = node_limit {
+ if let Some(limit) = options.node_limit {
cmd.arg("--max-count").arg(limit.to_string());
}
@@ -434,8 +507,17 @@ impl LocalFileSystem {
let mut out = GrepResult::new();
let mut killed_for_limit = false;
- let exclude_rel = exclude_path
+ let exclude_rel = options
+ .exclude_path
.and_then(|p| Self::exclude_to_query_root_relative_path(base_path, target_path, p));
+ let limit = options.node_limit.unwrap_or(usize::MAX);
+ let mut sink = LocalGrepSink::new(
+ String::new(),
+ options.before_context,
+ options.after_context,
+ limit,
+ &mut out,
+ );
#[derive(Debug, Deserialize)]
struct RgEvent {
@@ -474,8 +556,14 @@ impl LocalFileSystem {
Err(_) => continue,
};
- // Only handle "match" events.
- if ev.kind != "match" {
+ if ev.kind == "end" && sink.collector.match_count() >= limit {
+ let _ = child.kill();
+ killed_for_limit = true;
+ break;
+ }
+
+ let is_match = ev.kind == "match";
+ if !is_match && ev.kind != "context" {
continue;
}
@@ -510,7 +598,7 @@ impl LocalFileSystem {
}
}
- if let Some(limit) = level_limit {
+ if let Some(limit) = options.level_limit {
if Self::virtual_depth(&file_virtual) > limit {
continue;
}
@@ -525,18 +613,22 @@ impl LocalFileSystem {
.trim_end_matches(&['\r', '\n'][..])
.to_string();
- out.add_match(file_virtual, line_no, content);
-
- if let Some(limit) = node_limit {
- if out.count >= limit {
- // Reached global limit: terminate the rg process to avoid further scanning.
- let _ = child.kill();
- killed_for_limit = true;
- break;
- }
+ let keep_going = sink.consume_line(
+ &file_virtual,
+ GrepContextLine {
+ line: line_no,
+ content,
+ },
+ is_match,
+ );
+ if !keep_going {
+ let _ = child.kill();
+ killed_for_limit = true;
+ break;
}
}
+ drop(sink);
let status = child
.wait()
.map_err(|e| Error::InvalidOperation(format!("wait rg failed: {}", e)))?;
@@ -563,23 +655,19 @@ impl LocalFileSystem {
base_path: &Path,
virtual_path: &str,
pattern: &str,
- recursive: bool,
- case_insensitive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> Result<GrepResult> {
let local_root = Self::resolve_virtual_path(base_path, virtual_path);
if !local_root.exists() {
return Err(Error::NotFound(virtual_path.to_string()));
}
- if node_limit == Some(0) {
+ if options.node_limit == Some(0) {
return Ok(GrepResult::new());
}
// Build regex for fallback (uses Rust `regex` semantics).
- let regex_pattern = if case_insensitive {
+ let regex_pattern = if options.case_insensitive {
format!("(?i){}", pattern)
} else {
pattern.to_string()
@@ -591,44 +679,38 @@ impl LocalFileSystem {
let mut searcher = SearcherBuilder::new()
.line_number(true)
.binary_detection(BinaryDetection::quit(b'\x00'))
+ .before_context(options.before_context)
+ .after_context(options.after_context)
.build();
let mut out = GrepResult::new();
- let limit = node_limit.unwrap_or(usize::MAX);
- let exclude_rel = exclude_path
+ let limit = options.node_limit.unwrap_or(usize::MAX);
+ let exclude_rel = options
+ .exclude_path
.and_then(|p| Self::exclude_to_query_root_relative_path(base_path, &local_root, p));
// Single file: search directly.
if local_root.is_file() {
let file_virtual = Self::local_to_query_relative_path(&local_root, &local_root)
.ok_or_else(|| Error::InvalidPath(virtual_path.to_string()))?;
-
- let mut remaining = limit.saturating_sub(out.count);
- let sink = UTF8(|lnum, line| {
- if remaining == 0 {
- return Ok(false);
- }
- if let Some(excl_rel) = exclude_rel.as_deref() {
- if Self::is_excluded_virtual(&file_virtual, excl_rel) {
- return Ok(true);
- }
- }
- if let Some(max_depth) = level_limit {
- if Self::virtual_depth(&file_virtual) > max_depth {
- return Ok(true);
- }
- }
- out.add_match(
- file_virtual.clone(),
- lnum as u64,
- line.trim_end().to_string(),
- );
- remaining -= 1;
- Ok(remaining > 0)
- });
-
- // Similar to rg's --no-messages: file-level errors are not fatal here.
- let _ = searcher.search_path(&matcher, &local_root, sink);
+ if exclude_rel
+ .as_deref()
+ .is_some_and(|exclude| Self::is_excluded_virtual(&file_virtual, exclude))
+ || options
+ .level_limit
+ .is_some_and(|max_depth| Self::virtual_depth(&file_virtual) > max_depth)
+ {
+ return Ok(out);
+ }
+ Self::grep_file_via_searcher(
+ &mut searcher,
+ &matcher,
+ &local_root,
+ file_virtual,
+ options,
+ limit,
+ &mut out,
+ );
return Ok(out);
}
@@ -650,7 +732,7 @@ impl LocalFileSystem {
.git_exclude(true);
// Apply git-related ignore rules even if the target directory isn't inside a git repo.
builder.require_git(false);
- if !recursive && local_root.is_dir() {
+ if !options.recursive && local_root.is_dir() {
builder.max_depth(Some(1));
}
@@ -681,32 +763,45 @@ impl LocalFileSystem {
}
}
- if let Some(max_depth) = level_limit {
+ if let Some(max_depth) = options.level_limit {
if Self::virtual_depth(&file_virtual) > max_depth {
continue;
}
}
- let mut remaining = limit.saturating_sub(out.count);
- let sink = UTF8(|lnum, line| {
- if remaining == 0 {
- return Ok(false);
- }
- out.add_match(
- file_virtual.clone(),
- lnum as u64,
- line.trim_end().to_string(),
- );
- remaining -= 1;
- Ok(remaining > 0)
- });
-
- // Skip file read/permission errors to better match rg default behavior.
- let _ = searcher.search_path(&matcher, file_path, sink);
+ Self::grep_file_via_searcher(
+ &mut searcher,
+ &matcher,
+ file_path,
+ file_virtual,
+ options,
+ limit,
+ &mut out,
+ );
}
Ok(out)
}
+
+ fn grep_file_via_searcher(
+ searcher: &mut Searcher,
+ matcher: &RegexMatcher,
+ file_path: &Path,
+ file_virtual: String,
+ options: GrepOptions<'_>,
+ limit: usize,
+ result: &mut GrepResult,
+ ) {
+ let sink = LocalGrepSink::new(
+ file_virtual,
+ options.before_context,
+ options.after_context,
+ limit,
+ result,
+ );
+ // Similar to rg's --no-messages: file-level errors are not fatal here.
+ let _ = searcher.search_path(matcher, file_path, sink);
+ }
fn local_to_query_relative_path(query_root: &Path, local: &Path) -> Option<String> {
let rel = local.strip_prefix(query_root).ok()?;
let parts = rel
@@ -1286,14 +1381,10 @@ impl FileSystem for LocalFileSystem {
&self,
path: &str,
pattern: &str,
- recursive: bool,
- case_insensitive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> Result<GrepResult> {
// Fast-path: requesting 0 matches should return immediately without touching disk or spawning processes.
- if node_limit == Some(0) {
+ if options.node_limit == Some(0) {
return Ok(GrepResult::new());
}
@@ -1304,7 +1395,13 @@ impl FileSystem for LocalFileSystem {
let pattern_owned = pattern.to_string();
let path_owned = path.to_string();
let base_path = self.base_path.clone();
- let exclude_owned = exclude_path.map(|s| s.to_string());
+ let exclude_owned = options.exclude_path.map(str::to_string);
+ let recursive = options.recursive;
+ let case_insensitive = options.case_insensitive;
+ let node_limit = options.node_limit;
+ let level_limit = options.level_limit;
+ let before_context = options.before_context;
+ let after_context = options.after_context;
if self.has_rg {
// External rg path: run in a blocking thread to avoid blocking the async runtime.
@@ -1312,15 +1409,20 @@ impl FileSystem for LocalFileSystem {
let rg_base_path = base_path.clone();
let rg_exclude = exclude_owned.clone();
let rg_res = Self::run_blocking_fs(move || {
- LocalFileSystem::grep_via_rg(
- rg_base_path.as_path(),
- local.as_path(),
- rg_pattern.as_str(),
+ let options = GrepOptions {
recursive,
case_insensitive,
node_limit,
- rg_exclude.as_deref(),
+ exclude_path: rg_exclude.as_deref(),
level_limit,
+ before_context,
+ after_context,
+ };
+ LocalFileSystem::grep_via_rg(
+ rg_base_path.as_path(),
+ local.as_path(),
+ rg_pattern.as_str(),
+ options,
)
})
.await?;
@@ -1332,15 +1434,20 @@ impl FileSystem for LocalFileSystem {
// Fallback: also run in a blocking thread (dir walking + file reads are blocking IO).
Self::run_blocking_fs(move || {
- LocalFileSystem::grep_via_libs(
- base_path.as_path(),
- &path_owned,
- &pattern_owned,
+ let options = GrepOptions {
recursive,
case_insensitive,
node_limit,
- exclude_owned.as_deref(),
+ exclude_path: exclude_owned.as_deref(),
level_limit,
+ before_context,
+ after_context,
+ };
+ LocalFileSystem::grep_via_libs(
+ base_path.as_path(),
+ &path_owned,
+ &pattern_owned,
+ options,
)
})
.await
@@ -1399,29 +1506,6 @@ mod tests {
use std::path::Path;
use tempfile::TempDir;
- struct EnvVarGuard {
- key: &'static str,
- old: Option<std::ffi::OsString>,
- }
-
- impl EnvVarGuard {
- fn set(key: &'static str, val: &std::ffi::OsStr) -> Self {
- let old = std::env::var_os(key);
- std::env::set_var(key, val);
- Self { key, old }
- }
- }
-
- impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- if let Some(v) = self.old.take() {
- std::env::set_var(self.key, v);
- } else {
- std::env::remove_var(self.key);
- }
- }
- }
-
/// Create a LocalFS fixture pinned to the fallback grep implementation.
fn fallback_localfs() -> (TempDir, LocalFileSystem) {
let dir = TempDir::new().unwrap();
@@ -1552,27 +1636,6 @@ mod tests {
assert!(matches!(err, Error::WouldBlock(_)));
}
- /// Install a fake rg executable and prepend it to PATH for the current test.
- #[cfg(unix)]
- fn install_fake_rg(dir: &TempDir, script: &str) -> EnvVarGuard {
- let bin_dir = dir.path().join("bin");
- std::fs::create_dir_all(&bin_dir).unwrap();
- let rg_path = bin_dir.join("rg");
- std::fs::write(&rg_path, script).unwrap();
-
- use std::os::unix::fs::PermissionsExt;
- let mut perms = std::fs::metadata(&rg_path).unwrap().permissions();
- perms.set_mode(0o755);
- std::fs::set_permissions(&rg_path, perms).unwrap();
-
- let old_path = std::env::var_os("PATH").unwrap_or_default();
- let mut new_path = std::ffi::OsString::new();
- new_path.push(bin_dir.as_os_str());
- new_path.push(std::ffi::OsStr::new(":"));
- new_path.push(old_path);
- EnvVarGuard::set("PATH", &new_path)
- }
-
#[tokio::test]
async fn test_localfs_grep_fallback_respects_gitignore_and_hidden() {
let (dir, fs) = fallback_localfs();
@@ -1583,7 +1646,14 @@ mod tests {
write_file(dir.path(), ".hidden/secret.txt", "hello\n");
let result = fs
- .grep("/", "hello", true, false, None, None, None)
+ .grep(
+ "/",
+ "hello",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
@@ -1615,19 +1685,149 @@ mod tests {
write_file(dir.path(), "a.txt", "HELLO\n");
let result = fs
- .grep("/", "hello", true, true, None, None, None)
+ .grep(
+ "/",
+ "hello",
+ GrepOptions {
+ recursive: true,
+ case_insensitive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
assert_eq!(result.count, 1);
}
+ #[tokio::test]
+ async fn test_localfs_grep_includes_context() {
+ let (dir, fs) = fallback_localfs();
+ write_file(dir.path(), "a.txt", "first\nbefore\nhit\nafter\n");
+
+ let result = fs
+ .grep(
+ "/",
+ "hit",
+ GrepOptions {
+ recursive: true,
+ before_context: 2,
+ after_context: 1,
+ ..Default::default()
+ },
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(result.count, 1);
+ let matched = &result.matches[0];
+ assert_eq!(matched.line, 3);
+ assert_eq!(matched.before_context.as_ref().unwrap()[0].content, "first");
+ assert_eq!(
+ matched.before_context.as_ref().unwrap()[1].content,
+ "before"
+ );
+ assert_eq!(matched.after_context.as_ref().unwrap()[0].content, "after");
+ }
+
+ #[tokio::test]
+ async fn test_localfs_grep_context_handles_adjacent_matches_and_file_boundaries() {
+ let (dir, fs) = fallback_localfs();
+ write_file(dir.path(), "a.txt", "hit one\nhit two\nlast");
+
+ let result = fs
+ .grep(
+ "/",
+ "hit",
+ GrepOptions {
+ recursive: true,
+ before_context: 2,
+ after_context: 2,
+ ..Default::default()
+ },
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(result.count, 2);
+ assert_eq!(result.matches[0].before_context.as_ref().unwrap().len(), 0);
+ assert_eq!(
+ result.matches[0]
+ .after_context
+ .as_ref()
+ .unwrap()
+ .iter()
+ .map(|line| (line.line, line.content.as_str()))
+ .collect::<Vec<_>>(),
+ vec![(2, "hit two"), (3, "last")]
+ );
+ assert_eq!(
+ result.matches[1]
+ .before_context
+ .as_ref()
+ .unwrap()
+ .iter()
+ .map(|line| (line.line, line.content.as_str()))
+ .collect::<Vec<_>>(),
+ vec![(1, "hit one")]
+ );
+ assert_eq!(
+ result.matches[1]
+ .after_context
+ .as_ref()
+ .unwrap()
+ .iter()
+ .map(|line| (line.line, line.content.as_str()))
+ .collect::<Vec<_>>(),
+ vec![(3, "last")]
+ );
+ }
+
+ #[tokio::test]
+ async fn test_localfs_grep_node_limit_collects_trailing_context() {
+ let (dir, fs) = fallback_localfs();
+ write_file(dir.path(), "a.txt", "hit\none\ntwo\nhit\nthree\n");
+
+ let result = fs
+ .grep(
+ "/",
+ "hit",
+ GrepOptions {
+ recursive: true,
+ node_limit: Some(1),
+ after_context: 2,
+ ..Default::default()
+ },
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(result.count, 1);
+ assert_eq!(
+ result.matches[0]
+ .after_context
+ .as_ref()
+ .unwrap()
+ .iter()
+ .map(|line| (line.line, line.content.as_str()))
+ .collect::<Vec<_>>(),
+ vec![(2, "one"), (3, "two")]
+ );
+ }
+
#[tokio::test]
async fn test_localfs_grep_missing_path_returns_not_found() {
let (_dir, fs) = fallback_localfs();
let err = fs
- .grep("/does-not-exist", "hello", true, false, None, None, None)
+ .grep(
+ "/does-not-exist",
+ "hello",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap_err();
assert!(matches!(err, Error::NotFound(_)));
@@ -1647,7 +1847,14 @@ mod tests {
fs.has_rg = false;
let result = fs
- .grep("/", "hello", true, false, None, None, None)
+ .grep(
+ "/",
+ "hello",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
assert_eq!(result.count, 1);
@@ -1664,7 +1871,14 @@ mod tests {
fs.has_rg = has_rg;
for pattern in ["--version", "hello"] {
let result = fs
- .grep("/sub", pattern, true, false, None, None, None)
+ .grep(
+ "/sub",
+ pattern,
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
assert_eq!(result.count, 1);
@@ -1672,7 +1886,14 @@ mod tests {
assert_eq!(result.matches[0].content, "hello --version");
let single_file = fs
- .grep("/sub/-a.txt", pattern, true, false, None, None, None)
+ .grep(
+ "/sub/-a.txt",
+ pattern,
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
assert_eq!(single_file.count, 1);
@@ -1685,16 +1906,19 @@ mod tests {
#[cfg(unix)]
#[tokio::test]
async fn test_localfs_grep_node_limit_zero_does_not_invoke_rg() {
- let dir = TempDir::new().unwrap();
+ let (dir, mut fs) = fallback_localfs();
write_file(dir.path(), "a.txt", "hello\n");
-
- // Create a fake `rg` that fails if invoked. The expected behavior is to short-circuit
- // and return empty result when node_limit=0.
- let _path_guard = install_fake_rg(&dir, "#!/bin/sh\nexit 2\n");
-
- let fs = LocalFileSystem::new(dir.path().to_str().unwrap()).unwrap();
+ fs.has_rg = true;
let result = fs
- .grep("/", "hello", true, false, Some(0), None, None)
+ .grep(
+ "/",
+ "hello",
+ GrepOptions {
+ recursive: true,
+ node_limit: Some(0),
+ ..Default::default()
+ },
+ )
.await
.unwrap();
assert_eq!(result.count, 0);
@@ -1708,7 +1932,16 @@ mod tests {
write_file(dir.path(), "ok/y.txt", "hit\n");
let out = fs
- .grep("/", "hit", true, false, Some(1), Some("/excluded"), None)
+ .grep(
+ "/",
+ "hit",
+ GrepOptions {
+ recursive: true,
+ node_limit: Some(1),
+ exclude_path: Some("/excluded"),
+ ..Default::default()
+ },
+ )
.await
.unwrap();
@@ -1729,11 +1962,12 @@ mod tests {
.grep(
"/acct/resources/docs",
"hit",
- true,
- false,
- Some(1),
- Some("/acct/resources/docs/excluded"),
- None,
+ GrepOptions {
+ recursive: true,
+ node_limit: Some(1),
+ exclude_path: Some("/acct/resources/docs/excluded"),
+ ..Default::default()
+ },
)
.await
.unwrap();
@@ -1745,22 +1979,28 @@ mod tests {
#[cfg(unix)]
#[tokio::test]
- async fn test_localfs_grep_rg_fast_path_disables_parent_ignore_inheritance() {
+ async fn test_localfs_grep_rg_path_supports_context() {
let dir = TempDir::new().unwrap();
write_file(dir.path(), ".gitignore", "mount/\n");
let mount_dir = dir.path().join("mount");
- write_file(&mount_dir, "a.txt", "hello\n");
-
- // Fake `rg` verifies `--no-ignore-parent` is present, then emits one JSON match.
- let script = format!(
- "#!/bin/sh\nfor arg in \"$@\"; do\n if [ \"$arg\" = \"--no-ignore-parent\" ]; then\n printf '%s\\n' '{{\"type\":\"match\",\"data\":{{\"path\":{{\"text\":\"a.txt\"}},\"line_number\":1,\"lines\":{{\"text\":\"hello\\\\n\"}}}}}}'\n exit 0\n fi\ndone\necho missing --no-ignore-parent 1>&2\nexit 2\n"
- );
- let _path_guard = install_fake_rg(&dir, &script);
+ write_file(&mount_dir, "a.txt", "before\nhello\nafter\n");
let fs = LocalFileSystem::new(mount_dir.to_str().unwrap()).unwrap();
+ if !fs.has_rg {
+ return;
+ }
let result = fs
- .grep("/", "hello", true, false, None, None, None)
+ .grep(
+ "/",
+ "hello",
+ GrepOptions {
+ recursive: true,
+ before_context: 1,
+ after_context: 1,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
@@ -1768,6 +2008,14 @@ mod tests {
assert_eq!(result.matches.len(), 1);
assert_eq!(result.matches[0].file, "a.txt");
assert_eq!(result.matches[0].content, "hello");
+ assert_eq!(
+ result.matches[0].before_context.as_ref().unwrap()[0].content,
+ "before"
+ );
+ assert_eq!(
+ result.matches[0].after_context.as_ref().unwrap()[0].content,
+ "after"
+ );
}
#[tokio::test]
diff --git a/crates/ragfs/src/plugins/s3fs/mod.rs b/crates/ragfs/src/plugins/s3fs/mod.rs
index 4cc2297a5..8e2ee5826 100644
--- a/crates/ragfs/src/plugins/s3fs/mod.rs
+++ b/crates/ragfs/src/plugins/s3fs/mod.rs
@@ -31,9 +31,11 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use crate::core::context::FsContextView;
use crate::core::filesystem::{relative_depth, relative_match_file, sort_directory_entries};
use crate::core::glob::PreparedGlob;
+use crate::core::grep::GrepLineCollector;
+use crate::core::types::GrepContextLine;
use crate::core::{
- ConfigParameter, Error, FileInfo, FileSystem, GlobEntry, GlobPage, GrepMatch, GrepResult,
- PluginConfig, Result, ServicePlugin, TreeEntry, WriteFlag,
+ ConfigParameter, Error, FileInfo, FileSystem, GlobEntry, GlobPage, GrepMatch, GrepOptions,
+ GrepResult, PluginConfig, Result, ServicePlugin, TreeEntry, WriteFlag,
};
use tree::{build_tree_entries_from_flat_listing, build_tree_entries_from_listing_page, rel_parts};
@@ -104,92 +106,81 @@ async fn grep_stream(
file_size: u64,
re: &Regex,
remaining_limit: usize,
+ before_context: usize,
+ after_context: usize,
chunk_size: u64,
max_partial_size: usize,
reader: &mut dyn ChunkReader,
) -> Result<Vec<GrepMatch>> {
+ if remaining_limit == 0 {
+ return Ok(Vec::new());
+ }
+
let mut offset: u64 = 0;
- let mut partial = String::new();
+ let mut partial = Vec::new();
let mut line_no: u64 = 0;
- let mut matches = Vec::with_capacity(remaining_limit.min(64));
-
- loop {
- if matches.len() >= remaining_limit || offset >= file_size {
- break;
- }
-
+ let mut result = GrepResult {
+ matches: Vec::with_capacity(remaining_limit.min(64)),
+ count: 0,
+ };
+ let mut collector = GrepLineCollector::new(
+ rel_file.to_string(),
+ before_context,
+ after_context,
+ remaining_limit,
+ &mut result,
+ );
+
+ while offset < file_size {
let chunk = reader.read_chunk(offset, chunk_size).await?;
-
let is_last = chunk.is_empty()
|| chunk.len() < chunk_size as usize
- || offset + chunk_size >= file_size;
+ || offset + chunk.len() as u64 >= file_size;
+ offset += chunk.len() as u64;
+ partial.extend_from_slice(&chunk);
- let chunk_str = String::from_utf8_lossy(&chunk);
-
- let complete_end = if is_last {
- chunk_str.len()
- } else {
- chunk_str.rfind('\n').map(|p| p + 1).unwrap_or(0)
- };
-
- if complete_end == 0 && !is_last {
- partial.push_str(&chunk_str);
- if partial.len() > max_partial_size {
- return Ok(matches);
- }
- offset += chunk_size;
- continue;
+ if is_last && !partial.is_empty() && !partial.ends_with(b"\n") {
+ partial.push(b'\n');
}
- let (text, remainder) = if partial.is_empty() {
- if complete_end == chunk_str.len() {
- (chunk_str.into_owned(), String::new())
- } else {
- (
- chunk_str[..complete_end].to_string(),
- chunk_str[complete_end..].to_string(),
- )
- }
- } else {
- let merged = format!("{}{}", partial, &chunk_str[..complete_end]);
- partial.clear();
- (merged, chunk_str[complete_end..].to_string())
- };
-
- for line in text.lines() {
- if matches.len() >= remaining_limit {
- break;
- }
- line_no += 1;
- if re.is_match(line) {
- matches.push(GrepMatch {
- file: rel_file.to_string(),
- line: line_no,
- content: line.to_string(),
- });
+ if let Some(complete_end) = partial
+ .iter()
+ .rposition(|&byte| byte == b'\n')
+ .map(|p| p + 1)
+ {
+ let remainder = partial.split_off(complete_end);
+ let complete = std::mem::replace(&mut partial, remainder);
+ for line_bytes in complete[..complete.len() - 1].split(|&byte| byte == b'\n') {
+ line_no += 1;
+ let content = String::from_utf8_lossy(line_bytes)
+ .trim_end_matches('\r')
+ .to_string();
+ let is_match = re.is_match(&content);
+ if !collector.consume_line(
+ rel_file,
+ GrepContextLine {
+ line: line_no,
+ content,
+ },
+ is_match,
+ ) {
+ drop(collector);
+ return Ok(result.matches);
+ }
}
}
- partial.push_str(&remainder);
-
+ if partial.len() > max_partial_size {
+ drop(collector);
+ return Ok(result.matches);
+ }
if is_last {
break;
}
- offset += chunk_size;
- }
-
- if !partial.is_empty() && matches.len() < remaining_limit {
- line_no += 1;
- if re.is_match(&partial) {
- matches.push(GrepMatch {
- file: rel_file.to_string(),
- line: line_no,
- content: partial,
- });
- }
}
- Ok(matches)
+ drop(collector);
+ Ok(result.matches)
}
/// S3-backed file system
@@ -390,6 +381,8 @@ impl S3FileSystem {
file_size: u64,
re: &Regex,
remaining_limit: usize,
+ before_context: usize,
+ after_context: usize,
) -> Result<Vec<GrepMatch>> {
let normalized = Self::normalize_path(path);
let key = self.client.build_key(&normalized);
@@ -403,6 +396,8 @@ impl S3FileSystem {
file_size,
re,
remaining_limit,
+ before_context,
+ after_context,
Self::GREP_CHUNK_SIZE,
Self::GREP_MAX_PARTIAL_SIZE,
&mut reader,
@@ -417,9 +412,9 @@ impl S3FileSystem {
base_path: &str,
files: Vec<(String, u64)>,
re: &Regex,
- node_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> Result<GrepResult> {
- let limit = node_limit.unwrap_or(usize::MAX);
+ let limit = options.node_limit.unwrap_or(usize::MAX);
let result = Arc::new(Mutex::new(GrepResult::new()));
let matched_count = Arc::new(AtomicUsize::new(0));
let buf_size = Self::default_grep_concurrency().min(files.len());
@@ -436,7 +431,15 @@ impl S3FileSystem {
let remaining = limit.saturating_sub(done);
let matches = self
- .grep_one_file(base_path, &path, file_size, re, remaining)
+ .grep_one_file(
+ base_path,
+ &path,
+ file_size,
+ re,
+ remaining,
+ options.before_context,
+ options.after_context,
+ )
.await?;
let mut r = result.lock().unwrap();
@@ -936,18 +939,18 @@ impl FileSystem for S3FileSystem {
&self,
path: &str,
pattern: &str,
- recursive: bool,
- case_insensitive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> Result<GrepResult> {
let normalized = Self::normalize_path(path);
- let normalized_exclude = exclude_path.map(|p| Self::normalize_path(p));
+ let normalized_exclude = options.exclude_path.map(Self::normalize_path);
+ let normalized_options = GrepOptions {
+ exclude_path: normalized_exclude.as_deref(),
+ ..options
+ };
let info = self.stat(&normalized).await?;
- let re = if case_insensitive {
+ let re = if options.case_insensitive {
Regex::new(&format!("(?i){}", pattern))
} else {
Regex::new(pattern)
@@ -960,7 +963,7 @@ impl FileSystem for S3FileSystem {
return Ok(GrepResult::new());
}
}
- if let Some(limit) = level_limit {
+ if let Some(limit) = options.level_limit {
let rel = relative_match_file(&normalized, &normalized);
if relative_depth(&rel) > limit {
return Ok(GrepResult::new());
@@ -968,7 +971,7 @@ impl FileSystem for S3FileSystem {
}
let files = vec![(normalized.clone(), info.size)];
return self
- .grep_files_concurrent(&normalized, files, &re, node_limit)
+ .grep_files_concurrent(&normalized, files, &re, normalized_options)
.await;
}
@@ -995,14 +998,14 @@ impl FileSystem for S3FileSystem {
}
}
- if !recursive || level_limit.is_some() {
+ if !options.recursive || options.level_limit.is_some() {
let rel = relative_match_file(&normalized, &fs_path);
let depth = relative_depth(&rel);
- if !recursive && depth > 1 {
+ if !options.recursive && depth > 1 {
continue;
}
- if let Some(limit) = level_limit {
+ if let Some(limit) = options.level_limit {
if depth > limit {
continue;
}
@@ -1016,7 +1019,7 @@ impl FileSystem for S3FileSystem {
return Ok(GrepResult::new());
}
- self.grep_files_concurrent(&normalized, files, &re, node_limit)
+ self.grep_files_concurrent(&normalized, files, &re, normalized_options)
.await
}
@@ -1729,11 +1732,13 @@ mod tests {
/// Returns the range `[offset, offset+size)` clamped to `data.len()`.
struct MockChunkReader {
data: Vec<u8>,
+ reads: usize,
}
#[async_trait]
impl ChunkReader for MockChunkReader {
async fn read_chunk(&mut self, offset: u64, size: u64) -> Result<Vec<u8>> {
+ self.reads += 1;
let start = offset as usize;
if start >= self.data.len() {
return Ok(Vec::new());
@@ -1756,13 +1761,15 @@ mod tests {
) -> Result<Vec<GrepMatch>> {
let data = data.as_ref().to_vec();
let file_size = data.len() as u64;
- let mut reader = MockChunkReader { data };
+ let mut reader = MockChunkReader { data, reads: 0 };
let re = build_re(pattern);
grep_stream(
"f",
file_size,
&re,
100,
+ 0,
+ 0,
chunk_size,
max_partial,
&mut reader,
@@ -1839,6 +1846,88 @@ mod tests {
assert_eq!(matches[0].content, "lo wo", "stitched across boundary");
}
+ #[tokio::test]
+ async fn test_grep_stream_includes_context_across_chunks() {
+ let data = b"first\nbefore\nhit\nafter\n".to_vec();
+ let mut reader = MockChunkReader {
+ data: data.clone(),
+ reads: 0,
+ };
+ let matches = grep_stream(
+ "f",
+ data.len() as u64,
+ &build_re("hit"),
+ 100,
+ 2,
+ 1,
+ 8,
+ 1024,
+ &mut reader,
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(matches.len(), 1);
+ assert_eq!(matches[0].line, 3);
+ assert_eq!(
+ matches[0].before_context.as_ref().unwrap()[0].content,
+ "first"
+ );
+ assert_eq!(
+ matches[0].before_context.as_ref().unwrap()[1].content,
+ "before"
+ );
+ assert_eq!(
+ matches[0].after_context.as_ref().unwrap()[0].content,
+ "after"
+ );
+ }
+
+ #[tokio::test]
+ async fn test_grep_stream_stops_after_limited_match_context() {
+ let data = b"hit\none\ntwo\nunread\nunread\nunread\n".to_vec();
+ let mut reader = MockChunkReader {
+ data: data.clone(),
+ reads: 0,
+ };
+ let matches = grep_stream(
+ "f",
+ data.len() as u64,
+ &build_re("hit"),
+ 1,
+ 0,
+ 2,
+ 4,
+ 1024,
+ &mut reader,
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(matches.len(), 1);
+ assert_eq!(
+ matches[0]
+ .after_context
+ .as_ref()
+ .unwrap()
+ .iter()
+ .map(|line| line.content.as_str())
+ .collect::<Vec<_>>(),
+ vec!["one", "two"]
+ );
+ assert_eq!(reader.reads, 3);
+ }
+
+ #[tokio::test]
+ async fn test_grep_stream_preserves_utf8_split_across_chunks() {
+ let data = "before\n命中\nafter\n".as_bytes();
+ let matches = grep_chunks(data, "命中", 9, 1024).await.unwrap();
+
+ assert_eq!(matches.len(), 1);
+ assert_eq!(matches[0].line, 2);
+ assert_eq!(matches[0].content, "命中");
+ }
+
// ── Case 12: multiple consecutive chunks with no newline ──
#[tokio::test]
diff --git a/crates/ragfs/tests/cache_wrapper.rs b/crates/ragfs/tests/cache_wrapper.rs
index 4f1f2380c..4c31ab288 100644
--- a/crates/ragfs/tests/cache_wrapper.rs
+++ b/crates/ragfs/tests/cache_wrapper.rs
@@ -3,7 +3,9 @@ use ragfs::cache::{
CacheDecision, CacheNamespace, CachePolicy, CacheTraversalMode, CachedFileSystem,
};
use ragfs::cache_runtime::{CacheRuntime, MemoryMockProvider};
-use ragfs::core::{FsContextInner, GrepResult, MultiWriteWrappedFS, TreeEntry, FS_CTX};
+use ragfs::core::{
+ FsContextInner, GrepOptions, GrepResult, MultiWriteWrappedFS, TreeEntry, FS_CTX,
+};
use ragfs::plugins::MemFileSystem;
use ragfs::{Error, FileInfo, FileSystem, Result, WriteFlag};
use std::sync::atomic::{AtomicU64, Ordering};
@@ -129,24 +131,10 @@ impl FileSystem for CountingFileSystem {
&self,
path: &str,
pattern: &str,
- recursive: bool,
- case_insensitive: bool,
- node_limit: Option<usize>,
- exclude_path: Option<&str>,
- level_limit: Option<usize>,
+ options: GrepOptions<'_>,
) -> Result<GrepResult> {
self.greps.fetch_add(1, Ordering::Relaxed);
- self.inner
- .grep(
- path,
- pattern,
- recursive,
- case_insensitive,
- node_limit,
- exclude_path,
- level_limit,
- )
- .await
+ self.inner.grep(path, pattern, options).await
}
async fn tree_directory(
@@ -282,7 +270,14 @@ async fn default_grep_delegates_to_backend() {
let (fs, _) = cached_fs(backend);
let result = fs
- .grep("/docs", "needle", true, false, None, None, None)
+ .grep(
+ "/docs",
+ "needle",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
@@ -345,7 +340,14 @@ async fn cached_grep_traversal_reuses_directory_and_file_cache_after_warmup() {
);
let first = fs
- .grep("/docs", "needle", true, false, None, None, None)
+ .grep(
+ "/docs",
+ "needle",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
assert_eq!(first.count, 2);
@@ -355,7 +357,14 @@ async fn cached_grep_traversal_reuses_directory_and_file_cache_after_warmup() {
assert_eq!(probe.stat_count(), 1);
let second = fs
- .grep("/docs", "needle", true, false, None, None, None)
+ .grep(
+ "/docs",
+ "needle",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
assert_eq!(second.count, 2);
@@ -376,6 +385,47 @@ async fn cached_grep_traversal_reuses_directory_and_file_cache_after_warmup() {
);
}
+#[tokio::test]
+async fn cached_grep_includes_context_from_cached_content() {
+ let backend = CountingFileSystem::new();
+ backend.mkdir("/docs", 0o755).await.unwrap();
+ backend
+ .write(
+ "/docs/a.md",
+ b"first\nbefore\nneedle\nafter",
+ 0,
+ WriteFlag::Create,
+ )
+ .await
+ .unwrap();
+ let (fs, _) = cached_fs_with_policy(
+ backend,
+ CachePolicy::default().with_traversal_mode(CacheTraversalMode::CachedTraversal),
+ );
+
+ let result = fs
+ .grep(
+ "/docs",
+ "needle",
+ GrepOptions {
+ recursive: true,
+ before_context: 2,
+ after_context: 1,
+ ..Default::default()
+ },
+ )
+ .await
+ .unwrap();
+
+ let matched = &result.matches[0];
+ assert_eq!(matched.before_context.as_ref().unwrap()[0].content, "first");
+ assert_eq!(
+ matched.before_context.as_ref().unwrap()[1].content,
+ "before"
+ );
+ assert_eq!(matched.after_context.as_ref().unwrap()[0].content, "after");
+}
+
#[tokio::test]
async fn cached_grep_batches_generation_validation_after_warmup() {
let backend = CountingFileSystem::new();
@@ -394,13 +444,27 @@ async fn cached_grep_batches_generation_validation_after_warmup() {
CachePolicy::default().with_traversal_mode(CacheTraversalMode::CachedTraversal),
);
- fs.grep("/docs", "needle", true, false, None, None, None)
- .await
- .unwrap();
+ fs.grep(
+ "/docs",
+ "needle",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
+ .await
+ .unwrap();
provider.reset_observed_reads();
let result = fs
- .grep("/docs", "needle", true, false, None, None, None)
+ .grep(
+ "/docs",
+ "needle",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
@@ -429,13 +493,27 @@ async fn cached_grep_memoizes_generation_keys_within_one_traversal() {
CachePolicy::default().with_traversal_mode(CacheTraversalMode::CachedTraversal),
);
- fs.grep("/docs", "needle", true, false, None, None, None)
- .await
- .unwrap();
+ fs.grep(
+ "/docs",
+ "needle",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
+ .await
+ .unwrap();
provider.reset_observed_reads();
let result = fs
- .grep("/docs", "needle", true, false, None, None, None)
+ .grep(
+ "/docs",
+ "needle",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
@@ -477,13 +555,27 @@ async fn cached_grep_scans_cached_files_with_bounded_concurrency() {
provider,
);
- fs.grep("/docs", "needle", true, false, None, None, None)
- .await
- .unwrap();
+ fs.grep(
+ "/docs",
+ "needle",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
+ .await
+ .unwrap();
provider.reset_observed_reads();
let result = fs
- .grep("/docs", "needle", true, false, None, None, None)
+ .grep(
+ "/docs",
+ "needle",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
.await
.unwrap();
@@ -527,11 +619,14 @@ async fn cached_grep_traversal_matches_default_grep_semantics() {
.grep(
"/docs",
"needle",
- true,
- true,
- Some(2),
- Some("/docs/skip"),
- Some(1),
+ GrepOptions {
+ recursive: true,
+ case_insensitive: true,
+ node_limit: Some(2),
+ exclude_path: Some("/docs/skip"),
+ level_limit: Some(1),
+ ..Default::default()
+ },
)
.await
.unwrap();
@@ -539,11 +634,14 @@ async fn cached_grep_traversal_matches_default_grep_semantics() {
.grep(
"/docs",
"needle",
- true,
- true,
- Some(2),
- Some("/docs/skip"),
- Some(1),
+ GrepOptions {
+ recursive: true,
+ case_insensitive: true,
+ node_limit: Some(2),
+ exclude_path: Some("/docs/skip"),
+ level_limit: Some(1),
+ ..Default::default()
+ },
)
.await
.unwrap();
@@ -580,9 +678,16 @@ async fn cached_grep_traversal_falls_back_for_multiwrite_backend() {
let ctx = Arc::new(FsContextInner::new("acct"));
let result = FS_CTX
.scope(ctx, async {
- fs.grep("/docs", "needle", true, false, None, None, None)
- .await
- .unwrap()
+ fs.grep(
+ "/docs",
+ "needle",
+ GrepOptions {
+ recursive: true,
+ ..Default::default()
+ },
+ )
+ .await
+ .unwrap()
})
.await;
diff --git a/docs/en/api/06-retrieval.md b/docs/en/api/06-retrieval.md
index 7be8597e4..59bf36563 100644
--- a/docs/en/api/06-retrieval.md
+++ b/docs/en/api/06-retrieval.md
@@ -786,7 +786,7 @@ The `grep()` method performs regex pattern matching search in the file system, u
**Processing Pipeline**:
1. Traverse file system starting from specified URI
2. Perform regex matching on each file content
-3. Collect matching lines and position information
+3. Collect matching lines, position information, and optional surrounding context
4. Return matching results list
**Code Entry Points**:
@@ -808,6 +808,8 @@ The `grep()` method performs regex pattern matching search in the file system, u
| level_limit | int | No | Python SDK: 5; HTTP API / CLI / Go SDK: 10 | Maximum directory depth to traverse. The Go SDK currently uses the HTTP API default. |
| tags | string[] | No | Unset | Search only files matching every supplied `k=v` retrieval tag |
| include_tags | bool | No | `false` | Include each matched file's retrieval tags without filtering |
+| before_context | int | No | 0 | Number of context lines returned before each match; supported by the HTTP API and CLI |
+| after_context | int | No | 0 | Number of context lines returned after each match; supported by the HTTP API and CLI |
`tags` uses AND semantics and filters candidate files before content matching and `node_limit` truncation. For example, `["team=search", "env=prod"]` matches only files carrying both tags.
@@ -829,6 +831,8 @@ curl -X POST http://localhost:1933/api/v1/search/grep \
"uri": "viking://resources",
"pattern": "authentication",
"case_insensitive": true,
+ "before_context": 1,
+ "after_context": 1,
"tags": ["team=search", "env=prod"]
}'
```
@@ -890,6 +894,9 @@ openviking grep "authentication" --uri viking://resources --ignore-case
# Specify depth limit
openviking grep "TODO" --uri viking://resources --level-limit 3
+# Return two context lines before and after each match
+openviking grep "authentication" --uri viking://resources -b 2 -a 2
+
# Search only files carrying every tag
openviking grep "TODO" --uri viking://resources --tags team=search,env=prod
@@ -910,6 +917,12 @@ For HTTP `POST /api/v1/search/grep`, set `include_tags: true` to include tags wi
"uri": "viking://resources/docs/auth.md",
"line": 15,
"content": "User authentication is handled by...",
+ "before_context": [
+ {"line": 14, "content": "## Authentication"}
+ ],
+ "after_context": [
+ {"line": 16, "content": "Configure an API key before sending requests."}
+ ],
"tags": ["team=search", "env=prod"]
}
],
diff --git a/docs/zh/api/06-retrieval.md b/docs/zh/api/06-retrieval.md
index 467550cec..864d1c345 100644
--- a/docs/zh/api/06-retrieval.md
+++ b/docs/zh/api/06-retrieval.md
@@ -787,7 +787,7 @@ curl -X POST http://localhost:1933/api/v1/search/search \
**处理流程**:
1. 从指定 URI 开始遍历文件系统
2. 对每个文件内容进行正则表达式匹配
-3. 收集匹配的行和位置信息
+3. 收集匹配行、位置信息及可选的前后文
4. 返回匹配结果列表
**代码入口**:
@@ -809,6 +809,8 @@ curl -X POST http://localhost:1933/api/v1/search/search \
| level_limit | int | 否 | Python SDK: 5;HTTP API / CLI / Go SDK: 10 | 最大目录遍历深度。Go SDK 当前使用 HTTP API 默认值。 |
| tags | string[] | 否 | 未设置 | 仅搜索同时匹配全部 `k=v` 检索标签的文件 |
| include_tags | bool | 否 | `false` | 不过滤时也在每条命中中返回检索标签 |
+| before_context | int | 否 | 0 | 每条匹配行之前返回的上下文行数;仅 HTTP API 和 CLI 支持 |
+| after_context | int | 否 | 0 | 每条匹配行之后返回的上下文行数;仅 HTTP API 和 CLI 支持 |
`tags` 使用 AND 语义,并在内容匹配与 `node_limit` 截断之前过滤候选文件。例如 `["team=search", "env=prod"]` 只匹配同时具有两个标签的文件。
@@ -830,6 +832,8 @@ curl -X POST http://localhost:1933/api/v1/search/grep \
"uri": "viking://resources",
"pattern": "authentication",
"case_insensitive": true,
+ "before_context": 1,
+ "after_context": 1,
"tags": ["team=search", "env=prod"]
}'
```
@@ -891,6 +895,9 @@ openviking grep "authentication" --uri viking://resources --ignore-case
# 指定深度限制
openviking grep "TODO" --uri viking://resources --level-limit 3
+# 返回匹配行前后各 2 行上下文
+openviking grep "authentication" --uri viking://resources -b 2 -a 2
+
# 只搜索同时匹配所有 tags 的文件
openviking grep "TODO" --uri viking://resources --tags team=search,env=prod
@@ -911,6 +918,12 @@ HTTP `POST /api/v1/search/grep` 在不做过滤时可传 `include_tags: true`
"uri": "viking://resources/docs/auth.md",
"line": 15,
"content": "User authentication is handled by...",
+ "before_context": [
+ {"line": 14, "content": "## Authentication"}
+ ],
+ "after_context": [
+ {"line": 16, "content": "Configure an API key before sending requests."}
+ ],
"tags": ["team=search", "env=prod"]
}
],
diff --git a/openviking/server/routers/search.py b/openviking/server/routers/search.py
index 1edf20706..be8783846 100644
--- a/openviking/server/routers/search.py
+++ b/openviking/server/routers/search.py
@@ -329,6 +329,8 @@ class GrepRequest(BaseModel):
level_limit: int = 10
tags: Optional[List[str]] = None
include_tags: bool = False
+ before_context: int = Field(default=0, ge=0)
+ after_context: int = Field(default=0, ge=0)
class GlobRequest(BaseModel):
@@ -557,6 +559,8 @@ async def grep(
level_limit=request.level_limit,
tags=request.tags,
include_tags=request.include_tags,
+ before_context=request.before_context,
+ after_context=request.after_context,
)
except AGFSNotFoundError:
raise NotFoundError(resolved_uri, "file")
diff --git a/openviking/service/fs_service.py b/openviking/service/fs_service.py
index c81d47a85..119f45f81 100644
--- a/openviking/service/fs_service.py
+++ b/openviking/service/fs_service.py
@@ -1037,6 +1037,8 @@ class FSService:
level_limit: int = 10,
tags: Optional[List[str]] = None,
include_tags: bool = False,
+ before_context: int = 0,
+ after_context: int = 0,
) -> Dict:
"""Content search."""
viking_fs = self._ensure_initialized()
@@ -1054,6 +1056,8 @@ class FSService:
"ctx": ctx,
"tag_filter": tag_filter,
"include_tags": include_tags or bool(normalized_tags),
+ "before_context": before_context,
+ "after_context": after_context,
}
if _may_include_memory_content(uri):
kwargs["content_transform"] = _visible_grep_content
diff --git a/openviking/storage/viking_fs/_grep.py b/openviking/storage/viking_fs/_grep.py
index 34dbb01b8..61ec4bf32 100644
--- a/openviking/storage/viking_fs/_grep.py
+++ b/openviking/storage/viking_fs/_grep.py
@@ -35,6 +35,8 @@ class _GrepMixin:
allowed_uris: Optional[Set[str]] = None,
tag_filter: Optional[Dict[str, Any]] = None,
include_tags: bool = False,
+ before_context: int = 0,
+ after_context: int = 0,
) -> Dict:
"""Content search by pattern or keywords.
@@ -54,6 +56,8 @@ class _GrepMixin:
level_limit: Maximum depth level to traverse (default: 10)
ctx: Request context
content_transform: Optional projection applied before regex matching.
+ before_context: Number of lines to include before each match.
+ after_context: Number of lines to include after each match.
Internal bm25 recall limit is auto-adapted from node_limit as
min(node_limit * 5, 100000); when node_limit is unset, use 100000.
@@ -114,6 +118,8 @@ class _GrepMixin:
ctx=ctx,
content_transform=content_transform,
allowed_uris=allowed_uris,
+ before_context=before_context,
+ after_context=after_context,
)
else: # "vikingdb_then_fs"
result = await self._grep_vikingdb_then_fs(
@@ -127,6 +133,8 @@ class _GrepMixin:
allowed_uris=allowed_uris,
tag_filter=tag_filter,
include_tags=include_tags,
+ before_context=before_context,
+ after_context=after_context,
)
return self._attach_grep_tags(result, tags_by_uri)
@@ -233,6 +241,8 @@ class _GrepMixin:
ctx,
content_transform=None,
allowed_uris=None,
+ before_context=0,
+ after_context=0,
):
"""Filesystem grep path: prefer native agfs grep and fall back if unavailable."""
if content_transform is None and allowed_uris is None:
@@ -245,6 +255,8 @@ class _GrepMixin:
node_limit=node_limit,
level_limit=level_limit,
ctx=ctx,
+ before_context=before_context,
+ after_context=after_context,
)
except (AttributeError, AGFSNotSupportedError, NotImplementedError) as e:
logger.debug(f"agfs grep unavailable, falling back to VikingFS implementation: {e}")
@@ -259,6 +271,8 @@ class _GrepMixin:
ctx=ctx,
content_transform=content_transform,
allowed_uris=allowed_uris,
+ before_context=before_context,
+ after_context=after_context,
)
async def _grep_vikingdb_then_fs(
@@ -273,6 +287,8 @@ class _GrepMixin:
allowed_uris=None,
tag_filter=None,
include_tags=False,
+ before_context=0,
+ after_context=0,
):
"""VikingDB bm25 recall + local fs precise matching."""
vector_store = self._get_vector_store()
@@ -363,6 +379,8 @@ class _GrepMixin:
"node_limit": node_limit,
"level_limit": level_limit,
"ctx": ctx,
+ "before_context": before_context,
+ "after_context": after_context,
}
if allowed_uris is not None:
fallback_kwargs["allowed_uris"] = allowed_uris
@@ -390,6 +408,8 @@ class _GrepMixin:
case_insensitive,
node_limit,
ctx,
+ before_context,
+ after_context,
)
if tag_filter is None and not include_tags:
return grep_result
@@ -420,6 +440,8 @@ class _GrepMixin:
case_insensitive: bool,
node_limit: Optional[int],
ctx: Optional[RequestContext],
+ before_context: int = 0,
+ after_context: int = 0,
) -> Dict:
"""Execute regex matching in specified file list (vikingdb_then_fs Step 2)."""
flags = re.IGNORECASE if case_insensitive else 0
@@ -436,9 +458,18 @@ class _GrepMixin:
except Exception:
continue
- for line_no, line in enumerate(content.splitlines(), 1):
+ lines = content.splitlines()
+ for line_index, line in enumerate(lines):
if compiled.search(line):
- results.append({"uri": file_uri, "line": line_no, "content": line})
+ results.append(
+ self._build_grep_match(
+ file_uri,
+ lines,
+ line_index,
+ before_context,
+ after_context,
+ )
+ )
if node_limit and len(results) >= node_limit:
return {
"matches": results,
@@ -463,6 +494,8 @@ class _GrepMixin:
node_limit: Optional[int] = None,
level_limit: int = 10,
ctx: Optional[RequestContext] = None,
+ before_context: int = 0,
+ after_context: int = 0,
) -> Dict:
"""Grep using agfs native implementation.
@@ -481,6 +514,8 @@ class _GrepMixin:
node_limit: Maximum number of results to return
level_limit: Maximum depth level to traverse
ctx: Request context
+ before_context: Number of lines to include before each match
+ after_context: Number of lines to include after each match
Returns:
Dict with matches, count, match_count, files_scanned
@@ -503,6 +538,8 @@ class _GrepMixin:
node_limit=node_limit,
exclude_path=excluded_path,
level_limit=level_limit,
+ before_context=before_context,
+ after_context=after_context,
)
except (AttributeError, AGFSNotSupportedError, NotImplementedError):
# Capability missing: let the outer caller fall back to the VikingFS implementation.
@@ -542,13 +579,16 @@ class _GrepMixin:
files_scanned_set.add(file_uri)
- results.append(
- {
- "line": match.get("line", match.get("line_number", 0)),
- "uri": file_uri,
- "content": match.get("content", ""),
- }
- )
+ mapped_match = {
+ "line": match.get("line", match.get("line_number", 0)),
+ "uri": file_uri,
+ "content": match.get("content", ""),
+ }
+ if before_context > 0:
+ mapped_match["before_context"] = match.get("before_context", [])
+ if after_context > 0:
+ mapped_match["after_context"] = match.get("after_context", [])
+ results.append(mapped_match)
if node_limit and len(results) >= node_limit:
break
@@ -581,6 +621,8 @@ class _GrepMixin:
ctx: Optional[RequestContext] = None,
content_transform: Optional[Callable[[str, str], str]] = None,
allowed_uris: Optional[Set[str]] = None,
+ before_context: int = 0,
+ after_context: int = 0,
) -> Dict:
"""Grep implementation for encrypted files.
@@ -618,6 +660,8 @@ class _GrepMixin:
node_limit=node_limit,
ctx=ctx,
content_transform=content_transform,
+ before_context=before_context,
+ after_context=after_context,
)
return {
@@ -692,18 +736,24 @@ class _GrepMixin:
node_limit: Optional[int],
ctx: Optional[RequestContext] = None,
content_transform: Optional[Callable[[str, str], str]] = None,
+ before_context: int = 0,
+ after_context: int = 0,
) -> tuple[List[Dict[str, Any]], int]:
results: List[Dict[str, Any]] = []
files_scanned = 0
concurrency = _pkg()._DEFAULT_GREP_FILE_CONCURRENCY
for start in range(0, len(file_uris), concurrency):
batch_uris = file_uris[start : start + concurrency]
+ remaining_limit = node_limit - len(results) if node_limit else None
batch_jobs = [
self._grep_single_file(
entry_uri,
compiled_pattern,
ctx,
+ node_limit=remaining_limit,
content_transform=content_transform,
+ before_context=before_context,
+ after_context=after_context,
)
for entry_uri in batch_uris
]
@@ -722,7 +772,10 @@ class _GrepMixin:
entry_uri: str,
compiled_pattern: re.Pattern,
ctx: Optional[RequestContext] = None,
+ node_limit: Optional[int] = None,
content_transform: Optional[Callable[[str, str], str]] = None,
+ before_context: int = 0,
+ after_context: int = 0,
) -> tuple[List[Dict[str, Any]], int]:
try:
content = await self.read(entry_uri, ctx=ctx)
@@ -733,20 +786,51 @@ class _GrepMixin:
matches: List[Dict[str, Any]] = []
lines = content.split("\n")
- for line_num, line in enumerate(lines, 1):
+ for line_index, line in enumerate(lines):
if compiled_pattern.search(line):
matches.append(
- {
- "line": line_num,
- "uri": entry_uri,
- "content": line,
- }
+ self._build_grep_match(
+ entry_uri,
+ lines,
+ line_index,
+ before_context,
+ after_context,
+ )
)
+ if node_limit and len(matches) >= node_limit:
+ break
return matches, 1
except Exception as e:
logger.debug(f"Failed to grep {entry_uri}: {e}")
return [], 1
+ @staticmethod
+ def _build_grep_match(
+ uri: str,
+ lines: List[str],
+ line_index: int,
+ before_context: int,
+ after_context: int,
+ ) -> Dict[str, Any]:
+ match = {
+ "line": line_index + 1,
+ "uri": uri,
+ "content": lines[line_index],
+ }
+ if before_context > 0:
+ start = max(0, line_index - before_context)
+ match["before_context"] = [
+ {"line": index + 1, "content": lines[index]}
+ for index in range(start, line_index)
+ ]
+ if after_context > 0:
+ end = min(len(lines), line_index + after_context + 1)
+ match["after_context"] = [
+ {"line": index + 1, "content": lines[index]}
+ for index in range(line_index + 1, end)
+ ]
+ return match
+
def _resolve_grep_match_agfs_path(self, base_path: str, match_file: str) -> str:
"""Resolve a grep match path (relative to query root) into a full AGFS path."""
if match_file == ".":
diff --git a/tests/server/test_api_search.py b/tests/server/test_api_search.py
index 63425d749..be0a11cb5 100644
--- a/tests/server/test_api_search.py
+++ b/tests/server/test_api_search.py
@@ -994,10 +994,21 @@ async def test_grep(client_with_resource):
parent_uri = "/".join(uri.split("/")[:-1]) + "/"
resp = await client.post(
"/api/v1/search/grep",
- json={"uri": parent_uri, "pattern": "Sample"},
+ json={
+ "uri": parent_uri,
+ "pattern": "Introduction",
+ "before_context": 1,
+ "after_context": 1,
+ },
)
assert resp.status_code == 200
- assert resp.json()["status"] == "ok"
+ body = resp.json()
+ assert body["status"] == "ok"
+ match = body["result"]["matches"][0]
+ assert match["before_context"] == [{"line": 2, "content": ""}]
+ assert match["after_context"] == [
+ {"line": 4, "content": "This is a sample markdown document for server testing."}
+ ]
@pytest.mark.asyncio
@@ -1048,6 +1059,34 @@ async def test_grep_forwards_include_tags_to_filesystem_service(monkeypatch):
assert seen["include_tags"] is True
+async def test_grep_forwards_context_to_filesystem_service(monkeypatch):
+ seen = {}
+
+ async def fake_grep(uri, pattern, **kwargs):
+ seen.update(uri=uri, pattern=pattern, **kwargs)
+ return {"matches": [], "count": 0, "match_count": 0, "files_scanned": 0}
+
+ monkeypatch.setattr(
+ search_router,
+ "get_service",
+ lambda: SimpleNamespace(fs=SimpleNamespace(grep=fake_grep)),
+ )
+
+ await search_router.grep(
+ search_router.GrepRequest(
+ uri="viking://resources",
+ pattern="OpenViking",
+ before_context=2,
+ after_context=3,
+ ),
+ _ctx=RequestContext(user=UserIdentifier("acct", "alice"), role=Role.USER),
+ )
+
+ assert seen["before_context"] == 2
+ assert seen["after_context"] == 3
+
+
async def test_grep_case_insensitive(client_with_resource):
client, uri = client_with_resource
parent_uri = "/".join(uri.split("/")[:-1]) + "/"
diff --git a/tests/service/test_fs_service.py b/tests/service/test_fs_service.py
index b3edf547a..a09ed956e 100644
--- a/tests/service/test_fs_service.py
+++ b/tests/service/test_fs_service.py
@@ -305,6 +305,23 @@ async def test_grep_projects_memory_content_but_keeps_resource_fast_path(request
assert "content_transform" not in viking_fs.grep.await_args.kwargs
+async def test_grep_forwards_context_to_viking_fs(request_context):
+ viking_fs = SimpleNamespace(grep=AsyncMock(return_value={"matches": []}))
+ service = FSService(viking_fs=viking_fs)
+
+ await service.grep(
+ "viking://resources",
+ "needle",
+ ctx=request_context,
+ before_context=2,
+ after_context=3,
+ )
+
+ assert viking_fs.grep.await_args.kwargs["before_context"] == 2
+ assert viking_fs.grep.await_args.kwargs["after_context"] == 3
+
+
@pytest.mark.asyncio
async def test_grep_projects_tags_for_each_match(request_context):
matches = [
diff --git a/tests/storage/test_viking_fs_grep.py b/tests/storage/test_viking_fs_grep.py
index 5ff773060..412bc0db7 100644
--- a/tests/storage/test_viking_fs_grep.py
+++ b/tests/storage/test_viking_fs_grep.py
@@ -1,6 +1,7 @@
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0
+import re
import time
from unittest.mock import AsyncMock
@@ -145,7 +146,15 @@ async def test_grep_vikingdb_does_not_project_tags_without_filter_or_request(mon
)
monkeypatch.setattr(fs, "_get_vector_store", lambda: vector_store)
- async def fake_grep_in_files(file_uris, pattern, case_insensitive, node_limit, ctx):
+ async def fake_grep_in_files(
+ file_uris,
+ pattern,
+ case_insensitive,
+ node_limit,
+ ctx,
+ before_context,
+ after_context,
+ ):
return {
"matches": [{"uri": "viking://resources/a.md", "line": 1, "content": "needle"}],
"count": 1,
@@ -209,6 +218,8 @@ async def test_grep_vikingdb_remote_error_falls_back_to_fs(monkeypatch):
"node_limit": 10,
"level_limit": 3,
"ctx": None,
+ "before_context": 0,
+ "after_context": 0,
}
]
@@ -293,7 +304,15 @@ async def test_grep_vikingdb_keeps_local_exclude_uri_guard(monkeypatch):
grep_in_files_calls = []
- async def fake_grep_in_files(file_uris, pattern, case_insensitive, node_limit, ctx):
+ async def fake_grep_in_files(
+ file_uris,
+ pattern,
+ case_insensitive,
+ node_limit,
+ ctx,
+ before_context,
+ after_context,
+ ):
grep_in_files_calls.append(file_uris)
return {"matches": [], "count": 0, "match_count": 0, "files_scanned": len(file_uris)}
@@ -325,7 +344,15 @@ async def test_grep_vikingdb_pushes_tag_filter_into_bm25_request(monkeypatch):
calls = []
- async def fake_grep_in_files(file_uris, pattern, case_insensitive, node_limit, ctx):
+ async def fake_grep_in_files(
+ file_uris,
+ pattern,
+ case_insensitive,
+ node_limit,
+ ctx,
+ before_context,
+ after_context,
+ ):
calls.append(file_uris)
return {
"matches": [{"uri": "viking://resources/tagged.md", "line": 1, "content": "needle"}],
@@ -478,8 +505,11 @@ async def test_grep_applies_content_transform_before_matching(monkeypatch):
return [{"name": "memory.md", "isDir": False}]
return []
+ read_paths = []
+
def fake_agfs_read(path, offset=0, size=-1):
- return b'visible\n<!-- MEMORY_FIELDS {"secret":"hidden"} -->'
+ read_paths.append(path)
+ return b'before\nvisible\nafter\n<!-- MEMORY_FIELDS {"secret":"hidden"} -->'
monkeypatch.setattr(fs, "stat", fake_stat)
monkeypatch.setattr(fs, "ls", fake_ls)
@@ -494,15 +524,82 @@ async def test_grep_applies_content_transform_before_matching(monkeypatch):
"viking://resources",
pattern="visible|secret",
content_transform=lambda content, _uri: content.split("<!--", 1)[0].rstrip(),
+ before_context=2,
+ after_context=2,
)
assert result["matches"] == [
{
- "line": 1,
+ "line": 2,
"uri": "viking://resources/memory.md",
"content": "visible",
+ "before_context": [{"line": 1, "content": "before"}],
+ "after_context": [{"line": 3, "content": "after"}],
}
]
+ assert read_paths == ["/resources/memory.md"]
+
+
+async def test_grep_in_files_generates_context_from_single_read(monkeypatch):
+ fs = VikingFS(agfs=_DummyAgfs())
+ read = AsyncMock(return_value=b"first\nbefore\nmatch\nafter\nlast")
+ monkeypatch.setattr(fs, "read", read)
+
+ result = await fs._grep_in_files(
+ ["viking://resources/a.md"],
+ pattern="match",
+ case_insensitive=False,
+ node_limit=None,
+ ctx=None,
+ before_context=2,
+ after_context=1,
+ )
+
+ assert result["matches"] == [
+ {
+ "line": 3,
+ "uri": "viking://resources/a.md",
+ "content": "match",
+ "before_context": [
+ {"line": 1, "content": "first"},
+ {"line": 2, "content": "before"},
+ ],
+ "after_context": [{"line": 4, "content": "after"}],
+ }
+ ]
+ read.assert_awaited_once_with("viking://resources/a.md", ctx=None)
+
+
+async def test_grep_parallel_limits_context_construction_per_file(monkeypatch):
+ fs = VikingFS(agfs=_DummyAgfs())
+ lines = ["hit"] * 400
+ monkeypatch.setattr(fs, "read", AsyncMock(return_value="\n".join(lines)))
+
+ build_calls = 0
+ original_build_match = fs._build_grep_match
+
+ def count_build_calls(*args, **kwargs):
+ nonlocal build_calls
+ build_calls += 1
+ return original_build_match(*args, **kwargs)
+
+ monkeypatch.setattr(fs, "_build_grep_match", count_build_calls)
+
+ matches, files_scanned = await fs._grep_files_parallel(
+ ["viking://resources/a.md"],
+ compiled_pattern=re.compile("hit"),
+ node_limit=1,
+ before_context=400,
+ after_context=400,
+ )
+
+ assert files_scanned == 1
+ assert len(matches) == 1
+ assert build_calls == 1
+ assert matches[0]["before_context"] == []
+ assert len(matches[0]["after_context"]) == 399
@pytest.mark.asyncio
@@ -653,6 +750,59 @@ async def test_grep_delegates_to_agfs_with_expected_filters(monkeypatch, fs):
"node_limit": 10,
"exclude_path": "/resources/archive",
"level_limit": 3,
+ "before_context": 0,
+ "after_context": 0,
+ }
+ ]
+
+
+async def test_grep_with_context_uses_agfs_and_maps_context(monkeypatch, fs):
+ agfs_grep = AsyncMock(
+ return_value={
+ "matches": [
+ {
+ "file": "a.md",
+ "line": 2,
+ "content": "needle",
+ "before_context": [{"line": 1, "content": "before"}],
+ "after_context": [{"line": 3, "content": "after"}],
+ }
+ ],
+ "files_scanned": 1,
+ }
+ )
+ fallback = AsyncMock()
+ monkeypatch.setattr(fs._async_agfs, "grep", agfs_grep)
+ monkeypatch.setattr(fs, "_grep_encrypted", fallback)
+
+ result = await fs.grep(
+ "viking://resources",
+ pattern="needle",
+ before_context=1,
+ after_context=2,
+ )
+
+ agfs_grep.assert_awaited_once_with(
+ path="/resources",
+ pattern="needle",
+ recursive=True,
+ case_insensitive=False,
+ stream=False,
+ node_limit=None,
+ exclude_path=None,
+ level_limit=10,
+ before_context=1,
+ after_context=2,
+ )
+ fallback.assert_not_awaited()
+ assert result["matches"] == [
+ {
+ "line": 2,
+ "uri": "viking://resources/a.md",
+ "content": "needle",
+ "before_context": [{"line": 1, "content": "before"}],
+ "after_context": [{"line": 3, "content": "after"}],
}
]