imp-core 0.2.0

Agent engine for imp: loop, tools, sessions, hooks, context, and SDK
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! Boolean query parser with stemming for code search.
//!
//! Supports Elasticsearch-style queries:
//! - Boolean: "error AND handling", "login OR auth", "database NOT sqlite"
//! - Phrases: `"user authentication"` (exact match)
//! - Plain terms: matched with optional stemming
//!
//! Default operator between terms is OR.

use std::path::Path;

use regex::Regex;

/// A parsed search query with boolean semantics.
#[derive(Debug)]
pub struct Query {
    /// All must match (AND).
    pub must: Vec<Matcher>,
    /// At least one must match (OR / default).
    pub should: Vec<Matcher>,
    /// None may match (NOT).
    pub must_not: Vec<Matcher>,
}

/// A compiled matcher for a single term or phrase.
#[derive(Debug)]
pub struct Matcher {
    pub regex: Regex,
    pub original: String,
}

impl Query {
    /// Does this query match a given line?
    pub fn matches_line(&self, line: &str) -> bool {
        // All must_not must NOT match
        if self.must_not.iter().any(|m| m.regex.is_match(line)) {
            return false;
        }

        // All must terms must match
        let must_ok = self.must.is_empty() || self.must.iter().all(|m| m.regex.is_match(line));

        // At least one should term must match (if any exist)
        let should_ok =
            self.should.is_empty() || self.should.iter().any(|m| m.regex.is_match(line));

        must_ok && should_ok
    }

    /// Does this query match anywhere in a file's content?
    /// For AND semantics: each must term appears somewhere in the file.
    /// For OR semantics: at least one should term appears somewhere.
    /// For NOT: no must_not term appears anywhere.
    pub fn matches_file(&self, content: &str) -> bool {
        if self
            .must_not
            .iter()
            .any(|m| content.lines().any(|l| m.regex.is_match(l)))
        {
            return false;
        }

        let must_ok = self
            .must
            .iter()
            .all(|m| content.lines().any(|l| m.regex.is_match(l)));

        let should_ok = self.should.is_empty()
            || self
                .should
                .iter()
                .any(|m| content.lines().any(|l| m.regex.is_match(l)));

        must_ok && should_ok
    }

    /// Find lines in content that match any positive term.
    /// Used after `matches_file` confirms the file is relevant.
    pub fn matching_lines(&self, content: &str) -> Vec<usize> {
        content
            .lines()
            .enumerate()
            .filter(|(_, line)| {
                let any_positive = self.must.iter().any(|m| m.regex.is_match(line))
                    || self.should.iter().any(|m| m.regex.is_match(line));
                let no_negative = !self.must_not.iter().any(|m| m.regex.is_match(line));
                any_positive && no_negative
            })
            .map(|(idx, _)| idx)
            .collect()
    }

    /// Is this a simple single-term query? (optimization path)
    pub fn is_simple(&self) -> bool {
        self.must.is_empty() && self.must_not.is_empty() && self.should.len() == 1
    }

    /// Get the single regex for simple queries.
    pub fn simple_regex(&self) -> Option<&Regex> {
        if self.is_simple() {
            Some(&self.should[0].regex)
        } else {
            None
        }
    }
}

/// Parse a query string into a structured Query.
///
/// Syntax:
/// - `term` → OR term (default)
/// - `term1 AND term2` → both must match
/// - `term1 OR term2` → either must match
/// - `NOT term` → must not match
/// - `term1 AND NOT term2` → term1 required, term2 excluded
/// - `"exact phrase"` → literal phrase match
///
/// When `exact` is false, plain terms use stemming (suffix stripping + prefix match).
/// When `exact` is true, terms use word-boundary matching.
pub fn parse(input: &str, exact: bool, ignore_case: bool) -> std::result::Result<Query, String> {
    let tokens = tokenize(input);

    let mut must = Vec::new();
    let mut should = Vec::new();
    let mut must_not = Vec::new();
    let mut has_and = false;
    let mut next_not = false;

    // First pass: detect if AND is used (changes default grouping)
    for token in &tokens {
        if matches!(token, Token::And) {
            has_and = true;
            break;
        }
    }

    for token in tokens {
        match token {
            Token::And => {
                // AND is implicit in the grouping logic
            }
            Token::Or => {
                // Next term goes to should
            }
            Token::Not => {
                next_not = true;
            }
            Token::Term(term) => {
                let matcher = build_matcher(&term, false, exact, ignore_case)?;
                if next_not {
                    must_not.push(matcher);
                    next_not = false;
                } else if has_and {
                    must.push(matcher);
                } else {
                    should.push(matcher);
                }
            }
            Token::Phrase(phrase) => {
                let matcher = build_matcher(&phrase, true, true, ignore_case)?;
                if next_not {
                    must_not.push(matcher);
                    next_not = false;
                } else if has_and {
                    must.push(matcher);
                } else {
                    should.push(matcher);
                }
            }
        }
    }

    Ok(Query {
        must,
        should,
        must_not,
    })
}

fn build_matcher(
    term: &str,
    is_phrase: bool,
    exact: bool,
    ignore_case: bool,
) -> std::result::Result<Matcher, String> {
    let pattern = if is_phrase || exact {
        // Exact: escape and use word boundaries
        format!(r"\b{}\b", regex::escape(term))
    } else {
        // Stemmed: strip suffix, match as prefix
        let stemmed = stem(term);
        if stemmed.len() < term.len() {
            // Stem is shorter — match stem followed by optional word chars
            format!(r"(?i)\b{}\w*", regex::escape(&stemmed))
        } else {
            // No stemming applied — just case-insensitive match
            regex::escape(term)
        }
    };

    let regex = regex::RegexBuilder::new(&pattern)
        .case_insensitive(ignore_case || (!exact && !is_phrase))
        .build()
        .map_err(|e| format!("invalid pattern '{term}': {e}"))?;

    Ok(Matcher {
        regex,
        original: term.to_string(),
    })
}

// ── tokenizer ───────────────────────────────────────────────────────

#[derive(Debug, PartialEq)]
enum Token {
    Term(String),
    Phrase(String),
    And,
    Or,
    Not,
}

fn tokenize(input: &str) -> Vec<Token> {
    let mut tokens = Vec::new();
    let mut chars = input.chars().peekable();

    while let Some(&ch) = chars.peek() {
        if ch.is_whitespace() {
            chars.next();
            continue;
        }

        if ch == '"' {
            chars.next(); // consume opening quote
            let mut phrase = String::new();
            while let Some(&c) = chars.peek() {
                if c == '"' {
                    chars.next(); // consume closing quote
                    break;
                }
                phrase.push(c);
                chars.next();
            }
            if !phrase.is_empty() {
                tokens.push(Token::Phrase(phrase));
            }
            continue;
        }

        // Collect a word
        let mut word = String::new();
        while let Some(&c) = chars.peek() {
            if c.is_whitespace() || c == '"' {
                break;
            }
            word.push(c);
            chars.next();
        }

        match word.as_str() {
            "AND" => tokens.push(Token::And),
            "OR" => tokens.push(Token::Or),
            "NOT" => tokens.push(Token::Not),
            _ => tokens.push(Token::Term(word)),
        }
    }

    tokens
}

// ── stemming ────────────────────────────────────────────────────────

/// Simple English suffix stripping for code search.
///
/// Strips one layer of common suffixes to produce a stem that matches
/// related word forms. Conservative: requires at least 4 chars remaining
/// after stripping to avoid over-stemming.
///
/// Examples:
///   authenticate → authenticat (matches authentication, authenticated)
///   handling → handl (matches handler, handle, handled)
///   errors → error
///   connection → connect (matches connected, connecting)
pub fn stem(word: &str) -> String {
    let w = word.to_lowercase();

    // Ordered by suffix length (longest first) to strip the most specific suffix
    let suffixes = &[
        "ication", // authentication → authent
        "ation",   // authorization → authoriz
        "ition",   // definition → defin
        "ction",   // connection → conne  (try before "tion")
        "tion",    // completion → comple
        "sion",    // expression → expres
        "ment",    // management → manage
        "ness",    // readiness → readi
        "able",    // readable → read
        "ible",    // accessible → access
        "ally",    // automatically → automatic
        "ence",    // reference → refer
        "ance",    // performance → perform
        "ings",    // settings → sett
        "ated",    // authenticated → authentic
        "ized",    // authorized → author
        "ised",    // recognised → recogn
        "cting",   // connecting → conne (via "cting" before "ting")
        "less",    // careless → care
        "ful",     // successful → success
        "ous",     // dangerous → danger
        "ive",     // recursive → recurs
        "ity",     // security → secur
        "ing",     // handling → handl
        "ers",     // handlers → handl
        "ies",     // queries → quer
        "ied",     // applied → appl
        "ion",     // expression → express
        "ed",      // connected → connect, handled → handl
        "er",      // handler → handl
        "ate",     // authenticate → authentic\n        "es",      // matches → match
        "ly",      // directly → direct
        "al",      // functional → function
        "or",      // executor → execut
        "ar",      // similar → simil
        "s",       // errors → error
    ];

    let min_stem = 4;

    for suffix in suffixes {
        if w.len() > suffix.len() + min_stem && w.ends_with(suffix) {
            return w[..w.len() - suffix.len()].to_string();
        }
    }

    w
}

// ── language extension mapping ──────────────────────────────────────

/// Map a language name to file extensions for filtering.
pub fn language_extensions(language: &str) -> Option<&'static [&'static str]> {
    match language.to_lowercase().as_str() {
        "rust" | "rs" => Some(&["rs"]),
        "typescript" | "ts" => Some(&["ts", "tsx"]),
        "javascript" | "js" => Some(&["js", "jsx", "mjs", "cjs"]),
        "python" | "py" => Some(&["py", "pyi"]),
        "go" | "golang" => Some(&["go"]),
        "java" => Some(&["java"]),
        "ruby" | "rb" => Some(&["rb"]),
        "c" => Some(&["c", "h"]),
        "cpp" | "c++" | "cxx" => Some(&["cpp", "cc", "cxx", "hpp", "hxx", "h"]),
        "swift" => Some(&["swift"]),
        "php" => Some(&["php"]),
        "elixir" | "ex" => Some(&["ex", "exs"]),
        "zig" => Some(&["zig"]),
        "lua" => Some(&["lua"]),
        "shell" | "bash" | "sh" => Some(&["sh", "bash", "zsh"]),
        "toml" => Some(&["toml"]),
        "yaml" | "yml" => Some(&["yaml", "yml"]),
        "json" => Some(&["json"]),
        "markdown" | "md" => Some(&["md", "markdown"]),
        _ => None,
    }
}

/// Patterns that identify test files.
pub fn is_test_file(path: &Path) -> bool {
    let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
    let name_lower = name.to_lowercase();

    // Common test file patterns
    name_lower.ends_with("_test.go")
        || name_lower.ends_with("_test.rs")
        || name_lower.ends_with(".test.ts")
        || name_lower.ends_with(".test.tsx")
        || name_lower.ends_with(".test.js")
        || name_lower.ends_with(".test.jsx")
        || name_lower.ends_with(".spec.ts")
        || name_lower.ends_with(".spec.tsx")
        || name_lower.ends_with(".spec.js")
        || name_lower.ends_with(".spec.jsx")
        || name_lower.starts_with("test_")
        || name_lower == "conftest.py"
        || path.components().any(|c| {
            let s = c.as_os_str().to_str().unwrap_or("");
            s == "tests" || s == "test" || s == "__tests__" || s == "spec" || s == "specs"
        })
}

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

    #[test]
    fn tokenize_simple() {
        let tokens = tokenize("hello world");
        assert_eq!(
            tokens,
            vec![Token::Term("hello".into()), Token::Term("world".into())]
        );
    }

    #[test]
    fn tokenize_boolean() {
        let tokens = tokenize("error AND handling");
        assert_eq!(
            tokens,
            vec![
                Token::Term("error".into()),
                Token::And,
                Token::Term("handling".into())
            ]
        );
    }

    #[test]
    fn tokenize_not() {
        let tokens = tokenize("database NOT sqlite");
        assert_eq!(
            tokens,
            vec![
                Token::Term("database".into()),
                Token::Not,
                Token::Term("sqlite".into())
            ]
        );
    }

    #[test]
    fn tokenize_phrase() {
        let tokens = tokenize("\"user authentication\" AND error");
        assert_eq!(
            tokens,
            vec![
                Token::Phrase("user authentication".into()),
                Token::And,
                Token::Term("error".into())
            ]
        );
    }

    #[test]
    fn tokenize_or() {
        let tokens = tokenize("login OR auth");
        assert_eq!(
            tokens,
            vec![
                Token::Term("login".into()),
                Token::Or,
                Token::Term("auth".into())
            ]
        );
    }

    #[test]
    fn parse_and_query() {
        let q = parse("error AND handling", false, false).unwrap();
        assert_eq!(q.must.len(), 2);
        assert_eq!(q.should.len(), 0);
        assert!(q.matches_line("error handling here"));
        assert!(!q.matches_line("just an error"));
    }

    #[test]
    fn parse_or_query() {
        let q = parse("login OR auth", false, false).unwrap();
        assert_eq!(q.should.len(), 2);
        assert!(q.matches_line("login page"));
        assert!(q.matches_line("auth token"));
        assert!(!q.matches_line("nothing here"));
    }

    #[test]
    fn parse_not_query() {
        let q = parse("database NOT sqlite", false, false).unwrap();
        assert!(q.matches_line("database connection"));
        assert!(!q.matches_line("database sqlite connection"));
    }

    #[test]
    fn parse_phrase_query() {
        let q = parse("\"user authentication\"", true, false).unwrap();
        assert!(q.matches_line("the user authentication system"));
        assert!(!q.matches_line("the user and authentication"));
    }

    #[test]
    fn parse_simple_term() {
        let q = parse("ToolOutput", false, false).unwrap();
        assert!(q.is_simple());
        assert!(q.matches_line("fn text() -> ToolOutput {"));
    }

    #[test]
    fn stem_common_words() {
        assert_eq!(stem("authentication"), "authent");
        assert_eq!(stem("handling"), "handl");
        assert_eq!(stem("errors"), "error");
        assert_eq!(stem("handler"), "handl");
        assert_eq!(stem("performance"), "perform");
        // "connected": strips "ed" → "connect" (min_stem=4, 9>2+4)
        assert_eq!(stem("connected"), "connect");
    }

    #[test]
    fn stem_short_words_unchanged() {
        // Words too short to stem safely
        assert_eq!(stem("run"), "run");
        assert_eq!(stem("go"), "go");
        assert_eq!(stem("get"), "get");
    }

    #[test]
    fn stemmed_search_matches_variants() {
        let q = parse("authenticate", false, false).unwrap();
        // Should match via stemming: "authenticat" prefix
        assert!(q.matches_line("authentication required"));
        assert!(q.matches_line("authenticated user"));
        assert!(q.matches_line("fn authenticate()"));
    }

    #[test]
    fn exact_search_no_stemming() {
        let q = parse("authenticate", true, false).unwrap();
        assert!(q.matches_line("fn authenticate() {"));
        // Exact mode: word boundary, so "authentication" should NOT match
        assert!(!q.matches_line("authentication required"));
    }

    #[test]
    fn file_level_and_matching() {
        let content = "fn handle_error() {\n    log(\"something\");\n}\n\nfn setup_logging() {\n    // configure\n}";
        let q = parse("error AND logging", false, false).unwrap();
        assert!(q.matches_file(content));

        let q2 = parse("error AND database", false, false).unwrap();
        assert!(!q2.matches_file(content));
    }

    #[test]
    fn test_file_detection() {
        assert!(is_test_file(Path::new("src/auth_test.go")));
        assert!(is_test_file(Path::new("src/auth.test.ts")));
        assert!(is_test_file(Path::new("src/auth.spec.js")));
        assert!(is_test_file(Path::new("test_auth.py")));
        assert!(is_test_file(Path::new("tests/integration.rs")));
        assert!(is_test_file(Path::new("__tests__/auth.tsx")));
        assert!(!is_test_file(Path::new("src/auth.rs")));
        assert!(!is_test_file(Path::new("src/main.ts")));
    }

    #[test]
    fn language_extension_mapping() {
        assert_eq!(language_extensions("rust"), Some(&["rs"][..]));
        assert_eq!(language_extensions("typescript"), Some(&["ts", "tsx"][..]));
        assert_eq!(language_extensions("python"), Some(&["py", "pyi"][..]));
        assert_eq!(language_extensions("unknown"), None);
    }
}