cognis 0.2.0

LLM application framework built on cognis-core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Filesystem file search middleware — glob and grep search tools.
//!
//! Provides file search capabilities within a constrained root directory,
//! with path traversal protection.

use std::path::{Path, PathBuf};

use async_trait::async_trait;
use regex::Regex;
use serde::{Deserialize, Serialize};

use cognis_core::error::{CognisError, Result};

use super::types::AgentMiddleware;

/// Output mode for grep searches.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GrepOutputMode {
    /// Return matching lines with content.
    Content,
    /// Return only file paths that contain matches.
    FilesWithMatches,
}

/// Configuration for the filesystem file search middleware.
pub struct FilesystemFileSearchMiddleware {
    /// Root path that all searches are constrained to.
    pub root_path: PathBuf,
    /// Whether to use ripgrep (rg) for grep searches (falls back to internal grep).
    pub use_ripgrep: bool,
    /// Maximum file size in bytes to include in search results.
    pub max_file_size: usize,
}

impl FilesystemFileSearchMiddleware {
    pub fn new(root_path: impl Into<PathBuf>) -> Self {
        Self {
            root_path: root_path.into(),
            use_ripgrep: false,
            max_file_size: 10 * 1024 * 1024, // 10 MB
        }
    }

    pub fn with_ripgrep(mut self, use_ripgrep: bool) -> Self {
        self.use_ripgrep = use_ripgrep;
        self
    }

    pub fn with_max_file_size(mut self, max_size: usize) -> Self {
        self.max_file_size = max_size;
        self
    }

    /// Validate that a path is within the root directory (no traversal).
    pub fn validate_path(&self, path: &Path) -> Result<PathBuf> {
        // Resolve the path relative to root
        let resolved = if path.is_absolute() {
            path.to_path_buf()
        } else {
            self.root_path.join(path)
        };

        // Canonicalize would follow symlinks; for safety, normalize manually
        let normalized = normalize_path(&resolved);
        let root_normalized = normalize_path(&self.root_path);

        if !normalized.starts_with(&root_normalized) {
            return Err(CognisError::Other(format!(
                "Path traversal detected: '{}' is outside root '{}'",
                path.display(),
                self.root_path.display()
            )));
        }

        Ok(normalized)
    }

    /// Search for files matching a glob pattern within the root directory.
    ///
    /// Supports `**` patterns for recursive matching.
    /// An optional `path` parameter restricts the search to a subdirectory
    /// relative to the root.
    ///
    /// Returns a list of matching file paths relative to the root.
    pub fn glob_search(&self, pattern: &str) -> Result<Vec<String>> {
        self.glob_search_in(pattern, None)
    }

    /// Search for files matching a glob pattern within a subdirectory of root.
    pub fn glob_search_in(&self, pattern: &str, path: Option<&str>) -> Result<Vec<String>> {
        // Validate the pattern doesn't contain traversal
        if pattern.contains("..") {
            return Err(CognisError::Other(
                "Glob pattern must not contain '..'".into(),
            ));
        }

        let search_root = if let Some(p) = path {
            let sub = self.validate_path(Path::new(p))?;
            sub
        } else {
            self.root_path.clone()
        };

        let mut results = Vec::new();
        self.glob_walk_directory(&search_root, pattern, &mut results)?;

        Ok(results)
    }

    /// Recursively walk a directory and collect files matching a glob pattern.
    fn glob_walk_directory(
        &self,
        dir: &Path,
        pattern: &str,
        results: &mut Vec<String>,
    ) -> Result<()> {
        let entries = std::fs::read_dir(dir).map_err(|e| {
            CognisError::Other(format!(
                "Failed to read directory '{}': {}",
                dir.display(),
                e
            ))
        })?;

        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                // Skip hidden directories
                if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                    if !name.starts_with('.') {
                        self.glob_walk_directory(&path, pattern, results)?;
                    }
                }
            } else if path.is_file() {
                // Check file size
                if let Ok(metadata) = path.metadata() {
                    if metadata.len() > self.max_file_size as u64 {
                        continue;
                    }
                }

                if let Ok(rel) = path.strip_prefix(&self.root_path) {
                    let rel_str = rel.to_string_lossy().to_string();
                    if matches_glob_pattern(&rel_str, pattern) {
                        results.push(rel_str);
                    }
                }
            }
        }

        Ok(())
    }

    /// Search for a regex pattern within files in the root directory.
    ///
    /// Returns matches as (file_path, line_number, line_content) tuples.
    pub fn grep_search(&self, pattern: &str, file_pattern: Option<&str>) -> Result<Vec<GrepMatch>> {
        self.grep_search_in(pattern, file_pattern, None, GrepOutputMode::Content)
    }

    /// Search for a regex pattern within files, with optional subdirectory and output mode.
    ///
    /// - `path`: optional subdirectory relative to root to restrict the search.
    /// - `output_mode`: `Content` returns full matches, `FilesWithMatches` returns
    ///   one entry per file (with line_number 0 and empty content).
    pub fn grep_search_in(
        &self,
        pattern: &str,
        file_pattern: Option<&str>,
        path: Option<&str>,
        output_mode: GrepOutputMode,
    ) -> Result<Vec<GrepMatch>> {
        let re = Regex::new(pattern).map_err(|e| {
            CognisError::Other(format!("Invalid regex pattern '{}': {}", pattern, e))
        })?;

        let search_root = if let Some(p) = path {
            self.validate_path(Path::new(p))?
        } else {
            self.root_path.clone()
        };

        let mut results = Vec::new();
        self.search_directory(&search_root, &re, file_pattern, output_mode, &mut results)?;

        Ok(results)
    }

    fn search_directory(
        &self,
        dir: &Path,
        regex: &Regex,
        file_pattern: Option<&str>,
        output_mode: GrepOutputMode,
        results: &mut Vec<GrepMatch>,
    ) -> Result<()> {
        let entries = std::fs::read_dir(dir).map_err(|e| {
            CognisError::Other(format!(
                "Failed to read directory '{}': {}",
                dir.display(),
                e
            ))
        })?;

        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                // Recurse into subdirectories (skip hidden dirs)
                if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                    if !name.starts_with('.') {
                        self.search_directory(&path, regex, file_pattern, output_mode, results)?;
                    }
                }
            } else if path.is_file() {
                // Check file pattern filter
                if let Some(fp) = file_pattern {
                    let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
                    if !matches_simple_glob(name, fp) {
                        continue;
                    }
                }

                // Check file size
                if let Ok(metadata) = path.metadata() {
                    if metadata.len() > self.max_file_size as u64 {
                        continue;
                    }
                }

                // Read and search the file
                if let Ok(content) = std::fs::read_to_string(&path) {
                    let rel_path = path
                        .strip_prefix(&self.root_path)
                        .unwrap_or(&path)
                        .to_string_lossy()
                        .to_string();

                    match output_mode {
                        GrepOutputMode::Content => {
                            for (line_num, line) in content.lines().enumerate() {
                                if regex.is_match(line) {
                                    results.push(GrepMatch {
                                        file_path: rel_path.clone(),
                                        line_number: line_num + 1,
                                        line_content: line.to_string(),
                                    });
                                }
                            }
                        }
                        GrepOutputMode::FilesWithMatches => {
                            if regex.is_match(&content) {
                                results.push(GrepMatch {
                                    file_path: rel_path,
                                    line_number: 0,
                                    line_content: String::new(),
                                });
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }
}

/// A match found by grep search.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GrepMatch {
    /// File path relative to root.
    pub file_path: String,
    /// Line number (1-indexed).
    pub line_number: usize,
    /// Content of the matching line.
    pub line_content: String,
}

/// Normalize a path by resolving `.` and `..` components without filesystem access.
fn normalize_path(path: &Path) -> PathBuf {
    let mut components = Vec::new();
    for component in path.components() {
        match component {
            std::path::Component::ParentDir => {
                components.pop();
            }
            std::path::Component::CurDir => {}
            other => {
                components.push(other);
            }
        }
    }
    components.iter().collect()
}

/// Glob matching that supports `**` for recursive directory matching.
///
/// `**` matches any number of path segments (including zero).
/// `*` matches any characters except `/`.
fn matches_glob_pattern(text: &str, pattern: &str) -> bool {
    // Convert glob pattern to a regex
    let mut regex_str = String::from("^");
    let chars: Vec<char> = pattern.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        if i + 1 < chars.len() && chars[i] == '*' && chars[i + 1] == '*' {
            // `**` — match any path segments
            if i + 2 < chars.len() && chars[i + 2] == '/' {
                regex_str.push_str("(.*/)?");
                i += 3;
            } else {
                regex_str.push_str(".*");
                i += 2;
            }
        } else if chars[i] == '*' {
            // `*` — match anything except `/`
            regex_str.push_str("[^/]*");
            i += 1;
        } else if chars[i] == '?' {
            regex_str.push_str("[^/]");
            i += 1;
        } else {
            // Escape regex special chars
            let c = chars[i];
            if ".+^${}()|[]\\".contains(c) {
                regex_str.push('\\');
            }
            regex_str.push(c);
            i += 1;
        }
    }
    regex_str.push('$');

    Regex::new(&regex_str)
        .map(|re| re.is_match(text))
        .unwrap_or(false)
}

/// Simple glob matching (supports `*` as wildcard).
fn matches_simple_glob(text: &str, pattern: &str) -> bool {
    if !pattern.contains('*') {
        return text == pattern;
    }
    let parts: Vec<&str> = pattern.split('*').collect();
    if parts.is_empty() {
        return true;
    }

    let mut pos = 0;
    for (i, part) in parts.iter().enumerate() {
        if part.is_empty() {
            continue;
        }
        if let Some(found) = text[pos..].find(part) {
            if i == 0 && found != 0 {
                return false; // Must start with first part if no leading *
            }
            pos += found + part.len();
        } else {
            return false;
        }
    }
    // If no trailing *, the text must end exactly
    if !pattern.ends_with('*') {
        return text.ends_with(parts.last().unwrap_or(&""));
    }
    true
}

#[async_trait]
impl AgentMiddleware for FilesystemFileSearchMiddleware {
    fn name(&self) -> &str {
        "FilesystemFileSearchMiddleware"
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validate_path_valid() {
        let mw = FilesystemFileSearchMiddleware::new("/home/user/project");
        let result = mw.validate_path(Path::new("src/main.rs"));
        assert!(result.is_ok());
        let path = result.unwrap();
        assert!(path.starts_with("/home/user/project"));
    }

    #[test]
    fn test_validate_path_traversal_rejected() {
        let mw = FilesystemFileSearchMiddleware::new("/home/user/project");
        let result = mw.validate_path(Path::new("../../etc/passwd"));
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Path traversal"));
    }

    #[test]
    fn test_validate_path_absolute_within_root() {
        let mw = FilesystemFileSearchMiddleware::new("/home/user/project");
        let result = mw.validate_path(Path::new("/home/user/project/src/lib.rs"));
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_path_absolute_outside_root() {
        let mw = FilesystemFileSearchMiddleware::new("/home/user/project");
        let result = mw.validate_path(Path::new("/etc/passwd"));
        assert!(result.is_err());
    }

    #[test]
    fn test_normalize_path() {
        let path = normalize_path(Path::new("/a/b/../c/./d"));
        assert_eq!(path, PathBuf::from("/a/c/d"));
    }

    #[test]
    fn test_matches_simple_glob() {
        assert!(matches_simple_glob("hello.rs", "*.rs"));
        assert!(matches_simple_glob("hello.rs", "hello.*"));
        assert!(matches_simple_glob("hello.rs", "*"));
        assert!(!matches_simple_glob("hello.rs", "*.py"));
        assert!(matches_simple_glob("hello.rs", "hello.rs"));
    }

    #[test]
    fn test_glob_search_rejects_traversal() {
        let mw = FilesystemFileSearchMiddleware::new("/tmp/test");
        let result = mw.glob_search("../../etc/*");
        assert!(result.is_err());
    }

    #[test]
    fn test_middleware_name() {
        let mw = FilesystemFileSearchMiddleware::new("/tmp");
        assert_eq!(mw.name(), "FilesystemFileSearchMiddleware");
    }

    #[test]
    fn test_builder_methods() {
        let mw = FilesystemFileSearchMiddleware::new("/tmp")
            .with_ripgrep(true)
            .with_max_file_size(1024);
        assert!(mw.use_ripgrep);
        assert_eq!(mw.max_file_size, 1024);
    }

    #[test]
    fn test_grep_match_serde() {
        let m = GrepMatch {
            file_path: "src/main.rs".into(),
            line_number: 42,
            line_content: "fn main() {".into(),
        };
        let json = serde_json::to_string(&m).unwrap();
        let parsed: GrepMatch = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.line_number, 42);
    }
}