Skip to main content

pxh/recall/
engine.rs

1use std::path::PathBuf;
2
3use anyhow::Result;
4use bstr::BString;
5use nucleo::{Config, Matcher, Utf32Str};
6use rusqlite::Connection;
7
8use super::command::{FilterMode, HostFilter};
9use super::query::{RecallQuery, normalize_recall_char};
10
11/// A history entry with its metadata
12#[derive(Debug, Clone)]
13pub struct HistoryEntry {
14    pub id: i64,
15    pub command: String,
16    pub timestamp: Option<i64>,
17    pub working_directory: Option<BString>,
18    pub hostname: Option<BString>,
19    pub exit_status: Option<i32>,
20    pub duration_secs: Option<i64>,
21    /// Occurrences of this command within the loaded window. Rows load as 1;
22    /// the recall dedup pass accumulates collapsed duplicates here.
23    pub use_count: u32,
24}
25
26/// Search engine that combines SQLite queries with nucleo fuzzy matching
27pub struct SearchEngine {
28    conn: Connection,
29    working_directory: PathBuf,
30    host_set: Vec<BString>,
31    matcher: Matcher,
32    result_limit: usize,
33}
34
35impl SearchEngine {
36    pub fn new(
37        conn: Connection,
38        working_directory: PathBuf,
39        host_set: Vec<BString>,
40        result_limit: usize,
41    ) -> Self {
42        SearchEngine {
43            conn,
44            working_directory,
45            host_set,
46            matcher: Matcher::new(Config::DEFAULT),
47            result_limit,
48        }
49    }
50
51    /// Get the primary (current live) hostname -- used for display
52    pub fn primary_hostname(&self) -> &BString {
53        &self.host_set[0]
54    }
55
56    /// Check if a hostname is in this host's set (current + aliases)
57    pub fn is_this_host(&self, hostname: &BString) -> bool {
58        self.host_set.contains(hostname)
59    }
60
61    /// Append one LIKE condition per prefilter pattern of `query`. The
62    /// patterns come from `RecallQuery`, which owns the guarantee that they
63    /// accept every row the fuzzy stage could match.
64    fn push_query_conditions(
65        query: Option<&RecallQuery>,
66        where_conditions: &mut Vec<String>,
67        params: &mut Vec<String>,
68    ) {
69        for pattern in query.map(RecallQuery::like_patterns).unwrap_or_default() {
70            where_conditions
71                .push("CAST(full_command AS text) LIKE ? ESCAPE '\\' COLLATE NOCASE".to_string());
72            params.push(pattern);
73        }
74    }
75
76    /// Load history entries from the database, optionally narrowed by the
77    /// query's DB prefilter.
78    pub fn load_entries(
79        &self,
80        filter_mode: FilterMode,
81        host_filter: HostFilter,
82        query: Option<&RecallQuery>,
83    ) -> Result<Vec<HistoryEntry>> {
84        let (sql, params) = self.recall_query(filter_mode, host_filter, query);
85        let mut stmt = self.conn.prepare(&sql)?;
86        let param_refs: Vec<&dyn rusqlite::types::ToSql> =
87            params.iter().map(|s| s as &dyn rusqlite::types::ToSql).collect();
88        let entries = stmt
89            .query_map(param_refs.as_slice(), |row| self.row_to_entry(row))?
90            .collect::<Result<Vec<_>, _>>()?;
91        Ok(entries)
92    }
93
94    /// Build the recall SQL and its bound parameters. Oversamples by 3x and
95    /// relies on the caller's `deduplicate_entries()` for dedup -- avoids the
96    /// expensive CTE self-join that caused double table scans at scale. The
97    /// ORDER BY must stay satisfiable by `history_start_time` so LIMIT bounds
98    /// the work to the window rather than the table (see the plan tests).
99    fn recall_query(
100        &self,
101        filter_mode: FilterMode,
102        host_filter: HostFilter,
103        query: Option<&RecallQuery>,
104    ) -> (String, Vec<String>) {
105        let mut where_conditions = Vec::new();
106        let mut params: Vec<String> = Vec::new();
107
108        if filter_mode == FilterMode::Directory {
109            where_conditions.push("working_directory = CAST(? as blob)".to_string());
110            params.push(self.working_directory.to_string_lossy().to_string());
111        }
112
113        if host_filter == HostFilter::ThisHost {
114            let placeholders: String =
115                self.host_set.iter().map(|_| "CAST(? as blob)").collect::<Vec<_>>().join(", ");
116            where_conditions.push(format!("hostname IN ({placeholders})"));
117            params.extend(self.host_set.iter().map(ToString::to_string));
118        }
119
120        Self::push_query_conditions(query, &mut where_conditions, &mut params);
121
122        let where_clause = if where_conditions.is_empty() {
123            String::new()
124        } else {
125            format!("WHERE {}", where_conditions.join(" AND "))
126        };
127
128        let sql = format!(
129            r#"
130SELECT id, full_command, start_unix_timestamp, working_directory,
131       hostname, exit_status,
132       CASE WHEN end_unix_timestamp IS NOT NULL
133            THEN end_unix_timestamp - start_unix_timestamp
134            ELSE NULL END as duration
135  FROM command_history
136  {where_clause}
137 ORDER BY start_unix_timestamp DESC, id DESC
138 LIMIT {}
139"#,
140            self.result_limit * 3
141        );
142        (sql, params)
143    }
144
145    fn row_to_entry(&self, row: &rusqlite::Row) -> rusqlite::Result<HistoryEntry> {
146        let id: i64 = row.get(0)?;
147        let command: Vec<u8> = row.get(1)?;
148        let timestamp: Option<i64> = row.get(2)?;
149        let working_directory: Option<Vec<u8>> = row.get(3)?;
150        let hostname: Option<Vec<u8>> = row.get(4)?;
151        let exit_status: Option<i32> = row.get(5)?;
152        let duration_secs: Option<i64> = row.get(6)?;
153        Ok(HistoryEntry {
154            id,
155            command: String::from_utf8_lossy(&command).to_string(),
156            timestamp,
157            working_directory: working_directory.map(BString::from),
158            hostname: hostname.map(BString::from),
159            exit_status,
160            duration_secs,
161            use_count: 1,
162        })
163    }
164
165    /// Delete all history entries matching a command (trimmed), since the
166    /// recall list is deduplicated by command text.  Returns the number of
167    /// rows deleted.
168    pub fn delete_entries_by_command(&self, command: &str) -> Result<usize> {
169        let trimmed = command.trim_end();
170        let deleted = self.conn.execute(
171            "DELETE FROM command_history WHERE rtrim(CAST(full_command AS text)) = ?",
172            [trimmed],
173        )?;
174        Ok(deleted)
175    }
176
177    /// Get the configured result limit
178    pub fn result_limit(&self) -> usize {
179        self.result_limit
180    }
181
182    /// Get the working directory for display
183    pub fn working_directory(&self) -> &PathBuf {
184        &self.working_directory
185    }
186
187    /// Filter entries using nucleo fuzzy matching.
188    /// Returns (index, highlight_positions) sorted by combined score:
189    /// nucleo's fuzzy score (word-boundary matches favored, gaps penalized)
190    /// plus a recency boost so freshly-used commands outrank stale ones at
191    /// similar match quality. Original-index ascending breaks remaining ties
192    /// (more recent first).
193    pub fn filter_entries(
194        &mut self,
195        entries: &[HistoryEntry],
196        query: &RecallQuery,
197    ) -> Vec<(usize, Vec<u32>)> {
198        if query.is_empty() {
199            // No query - return all entries without match positions
200            return (0..entries.len()).map(|i| (i, Vec::new())).collect();
201        }
202
203        let now_secs = crate::unix_now();
204
205        // Nucleo distinguishes whitespace boundaries (BONUS_BOUNDARY_WHITE) from
206        // delimiter boundaries (BONUS_BOUNDARY_DELIMITER, ~40% smaller), so for query
207        // "foobar" the haystack `cat /foobar` scores meaningfully below `foobar plain`
208        // -- enough that the frecency boost can't always close the gap. Normalizing
209        // `-`, `*`, `/` to spaces in both query and haystack puts these match positions
210        // in the BOUNDARY_WHITE tier. (`*` also acts as a word separator in queries.)
211        // The original query is used for highlighting so `--release` shows highlighted dashes.
212        let scoring_pattern = query.scoring_pattern();
213        let highlight_pattern = query.highlight_pattern();
214
215        let mut scored_results: Vec<(usize, u32, Vec<u32>)> = Vec::new();
216        let mut buf = Vec::new();
217        let mut normalized_cmd = String::new();
218        let cwd = self.working_directory.as_os_str().as_encoded_bytes();
219
220        for (original_idx, entry) in entries.iter().enumerate() {
221            // Normalize command for scoring (-, *, / → space)
222            normalized_cmd.clear();
223            normalized_cmd.extend(entry.command.chars().map(normalize_recall_char));
224            buf.clear();
225            let haystack = Utf32Str::new(&normalized_cmd, &mut buf);
226
227            if let Some(score) = scoring_pattern.score(haystack, &mut self.matcher) {
228                // Get highlight indices from original command/query, with fallback to scoring
229                // pattern if original query doesn't match (e.g., query "--release" vs cmd "release")
230                let mut indices = Vec::new();
231                buf.clear();
232                let haystack = Utf32Str::new(&entry.command, &mut buf);
233                highlight_pattern.indices(haystack, &mut self.matcher, &mut indices);
234                if indices.is_empty() {
235                    buf.clear();
236                    let haystack = Utf32Str::new(&entry.command, &mut buf);
237                    scoring_pattern.indices(haystack, &mut self.matcher, &mut indices);
238                }
239                let boosted = score
240                    + frecency_boost(entry.timestamp, now_secs)
241                    + frequency_boost(entry.use_count)
242                    + directory_boost(entry.working_directory.as_ref().map(|d| d.as_slice()), cwd);
243                scored_results.push((original_idx, boosted, indices));
244            }
245        }
246
247        // Sort by score (descending), then by original index (ascending = more recent first)
248        scored_results.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
249
250        // Return just the indices and highlight positions
251        scored_results.into_iter().map(|(idx, _, indices)| (idx, indices)).collect()
252    }
253}
254
255/// Recency boost added to nucleo's fuzzy score so freshly-used commands outrank
256/// stale ones at similar match quality. The step values are tuned to nucleo's
257/// natural range (~16-32 for single-char queries): the boost can flip ordering
258/// for short queries, but for longer queries -- where nucleo scores grow with
259/// each matched char -- it degrades gracefully into a tiebreaker.
260fn frecency_boost(timestamp: Option<i64>, now_secs: i64) -> u32 {
261    let Some(ts) = timestamp else { return 0 };
262    let age_secs = (now_secs - ts).max(0);
263    let age_days = age_secs as f64 / 86400.0;
264    match age_days {
265        d if d < 0.04 => 24, // < ~1 hour
266        d if d < 1.0 => 16,
267        d if d < 7.0 => 10,
268        d if d < 30.0 => 5,
269        d if d < 90.0 => 2,
270        _ => 0,
271    }
272}
273
274/// Boost for commands used repeatedly within the loaded window, so workhorse
275/// commands outrank one-offs at similar match quality. Log-scale and capped
276/// below a single matched char (~16 in nucleo's scale) so frequency can break
277/// near-ties but never swamp match quality: 1 use → 0, 2-3 → 4, 4-7 → 8,
278/// 8+ → 12.
279fn frequency_boost(use_count: u32) -> u32 {
280    (4 * use_count.max(1).ilog2()).min(12)
281}
282
283/// Boost for commands last run in the current working directory, so global
284/// mode softly prefers project-local history without hiding everything else
285/// (the directory filter mode remains the hard version). In directory mode
286/// every entry shares the cwd, making the boost uniform and rank-neutral.
287fn directory_boost(working_directory: Option<&[u8]>, cwd: &[u8]) -> u32 {
288    if !cwd.is_empty() && working_directory == Some(cwd) { 8 } else { 0 }
289}
290
291/// Format a timestamp as a relative time string (e.g., "2m", "3h", "2d")
292pub fn format_relative_time(timestamp: Option<i64>) -> String {
293    let Some(ts) = timestamp else {
294        return "   ".to_string();
295    };
296
297    let now = crate::unix_now();
298
299    let diff = now - ts;
300    if diff < 0 {
301        return "   ".to_string();
302    }
303
304    let diff = diff as u64;
305    if diff < 60 {
306        format!("{diff:>2}s")
307    } else if diff < 3600 {
308        format!("{:>2}m", diff / 60)
309    } else if diff < 86400 {
310        format!("{:>2}h", diff / 3600)
311    } else if diff < 86400 * 7 {
312        format!("{:>2}d", diff / 86400)
313    } else if diff < 86400 * 30 {
314        format!("{:>2}w", diff / (86400 * 7))
315    } else if diff < 86400 * 365 {
316        format!("{:>2}M", diff / (86400 * 30))
317    } else {
318        format!("{:>2}y", diff / (86400 * 365))
319    }
320}
321
322#[cfg(test)]
323mod tests {
324    use std::path::PathBuf;
325
326    use bstr::BString;
327    use rusqlite::Connection;
328
329    use super::*;
330
331    fn test_db() -> Connection {
332        let conn = Connection::open_in_memory().unwrap();
333        crate::initialize_base_schema(&conn).unwrap();
334        crate::run_schema_migrations(&conn).unwrap();
335        conn
336    }
337
338    fn q(query: &str) -> RecallQuery {
339        RecallQuery::parse(query)
340    }
341
342    fn insert_command(conn: &Connection, cmd: &str, hostname: &str, dir: &str, ts: i64) {
343        conn.execute(
344            "INSERT INTO command_history (session_id, full_command, shellname, hostname, working_directory, start_unix_timestamp)
345             VALUES (1, CAST(? AS blob), 'bash', CAST(? AS blob), CAST(? AS blob), ?)",
346            rusqlite::params![cmd, hostname, dir, ts],
347        )
348        .unwrap();
349    }
350
351    #[test]
352    fn test_engine_host_filter() {
353        let conn = test_db();
354        insert_command(&conn, "alpha-cmd", "alpha", "/tmp", 1000);
355        insert_command(&conn, "beta-cmd", "beta", "/tmp", 2000);
356
357        let engine =
358            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("alpha")], 100);
359        let entries = engine.load_entries(FilterMode::Global, HostFilter::ThisHost, None).unwrap();
360        assert_eq!(entries.len(), 1);
361        assert_eq!(entries[0].command, "alpha-cmd");
362
363        let entries = engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
364        assert_eq!(entries.len(), 2);
365    }
366
367    #[test]
368    fn test_engine_directory_filter() {
369        let conn = test_db();
370        insert_command(&conn, "in-project", "host1", "/home/user/project", 1000);
371        insert_command(&conn, "in-other", "host1", "/home/user/other", 2000);
372
373        let engine = SearchEngine::new(
374            conn,
375            PathBuf::from("/home/user/project"),
376            vec![BString::from("host1")],
377            100,
378        );
379        let entries =
380            engine.load_entries(FilterMode::Directory, HostFilter::AllHosts, None).unwrap();
381        assert_eq!(entries.len(), 1);
382        assert_eq!(entries[0].command, "in-project");
383    }
384
385    #[test]
386    fn test_engine_returns_all_rows_ordered_by_time() {
387        // load_entries returns raw rows (most recent first); dedup is the caller's job.
388        let conn = test_db();
389        insert_command(&conn, "ls -la", "host1", "/tmp", 1000);
390        insert_command(&conn, "ls -la", "host1", "/tmp", 2000);
391        insert_command(&conn, "pwd", "host1", "/tmp", 1500);
392
393        let engine =
394            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
395        let entries = engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
396        assert_eq!(entries.len(), 3);
397        assert_eq!(entries[0].command, "ls -la");
398        assert_eq!(entries[0].timestamp, Some(2000));
399        assert_eq!(entries[1].command, "pwd");
400        assert_eq!(entries[1].timestamp, Some(1500));
401    }
402
403    #[test]
404    fn test_engine_fuzzy_normalization_dashes() {
405        let conn = test_db();
406        insert_command(&conn, "cargo build --release", "host1", "/tmp", 1000);
407        insert_command(&conn, "echo hello", "host1", "/tmp", 2000);
408
409        let mut engine =
410            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
411        let entries = engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
412
413        let filtered = engine.filter_entries(&entries, &q("release"));
414        assert_eq!(filtered.len(), 1);
415        assert_eq!(entries[filtered[0].0].command, "cargo build --release");
416
417        let filtered = engine.filter_entries(&entries, &q("--release"));
418        assert_eq!(filtered.len(), 1);
419        assert_eq!(entries[filtered[0].0].command, "cargo build --release");
420    }
421
422    #[test]
423    fn test_engine_fuzzy_normalization_asterisks() {
424        let conn = test_db();
425        insert_command(&conn, "find . -name '*.rs'", "host1", "/tmp", 1000);
426        insert_command(&conn, "echo hello", "host1", "/tmp", 2000);
427
428        let mut engine =
429            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
430        let entries = engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
431
432        let filtered = engine.filter_entries(&entries, &q("*.rs"));
433        assert_eq!(filtered.len(), 1);
434        assert!(entries[filtered[0].0].command.contains("*.rs"));
435    }
436
437    #[test]
438    fn test_engine_fuzzy_normalization_slashes() {
439        let conn = test_db();
440        insert_command(&conn, "cat /foobar", "host1", "/tmp", 1000);
441        insert_command(&conn, "echo hello", "host1", "/tmp", 2000);
442
443        let mut engine =
444            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
445        let entries = engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
446
447        let filtered = engine.filter_entries(&entries, &q("foobar"));
448        assert_eq!(filtered.len(), 1);
449        assert_eq!(entries[filtered[0].0].command, "cat /foobar");
450    }
451
452    #[test]
453    fn test_engine_fzf_operator_semantics() {
454        // Locks in the fzf-style query operators documented in the README:
455        // `'exact`, `^prefix`, `suffix$`, `!negation`, and combinations.
456        let conn = test_db();
457        let commands = [
458            "sudo apt install ripgrep",
459            "cargo build --release",
460            "watch cargo test",
461            "git push",
462            "git push origin main",
463            "grep -rn limit src",
464        ];
465        for (i, cmd) in commands.iter().enumerate() {
466            insert_command(&conn, cmd, "host1", "/tmp", 1000 + i as i64);
467        }
468
469        let mut engine =
470            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
471        let entries = engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
472
473        let matches = |engine: &mut SearchEngine, query: &str| -> Vec<String> {
474            let mut cmds: Vec<String> = engine
475                .filter_entries(&entries, &q(query))
476                .iter()
477                .map(|(idx, _)| entries[*idx].command.clone())
478                .collect();
479            cmds.sort();
480            cmds
481        };
482
483        // Fuzzy `git` also matches the subsequence in `grep -rn limit src`;
484        // `'git` requires the exact substring.
485        assert_eq!(
486            matches(&mut engine, "git"),
487            ["git push", "git push origin main", "grep -rn limit src"]
488        );
489        assert_eq!(matches(&mut engine, "'git"), ["git push", "git push origin main"]);
490
491        assert_eq!(matches(&mut engine, "^cargo"), ["cargo build --release"]);
492        assert_eq!(matches(&mut engine, "push$"), ["git push"]);
493        assert_eq!(matches(&mut engine, "^git push$"), ["git push"]);
494
495        let without_sudo = matches(&mut engine, "!sudo");
496        assert_eq!(without_sudo.len(), 5);
497        assert!(!without_sudo.contains(&"sudo apt install ripgrep".to_string()));
498    }
499
500    #[test]
501    fn test_filter_entries_recent_slash_path_beats_older_plain_word() {
502        // The user's reported bug: typing `foobar` should rank a recent
503        // `cat /foobar` (path-component match) above an old plain `foobar`.
504        // Without `/` normalization, nucleo's delimiter-boundary bonus is
505        // smaller than the whitespace-boundary bonus, and the gap can exceed
506        // the frecency boost cap.
507        let conn = test_db();
508        let now = wall_clock_now();
509
510        insert_command(&conn, "foobar --plain", "host1", "/tmp", now - 86_400 * 200);
511        insert_command(&conn, "cat /foobar", "host1", "/tmp", now - 60);
512
513        let mut engine =
514            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
515        let entries = engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
516        let filtered = engine.filter_entries(&entries, &q("foobar"));
517
518        assert_eq!(filtered.len(), 2);
519        assert_eq!(
520            entries[filtered[0].0].command, "cat /foobar",
521            "recent /foobar should outrank older plain foobar once `/` normalizes to a word boundary"
522        );
523    }
524
525    #[test]
526    fn test_like_filter_normalizes_slash() {
527        // Query `/foobar` should match haystacks where `/foobar` doesn't appear
528        // literally -- the `/` is a wildcard in the prefilter, consistent with
529        // how it's treated as a word separator in scoring.
530        let conn = test_db();
531        insert_command(&conn, "echo foobar", "host1", "/tmp", 1000);
532
533        let engine =
534            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
535        let entries = engine
536            .load_entries(FilterMode::Global, HostFilter::AllHosts, Some(&q("/foobar")))
537            .unwrap();
538        assert_eq!(entries.len(), 1);
539        assert_eq!(entries[0].command, "echo foobar");
540    }
541
542    #[test]
543    fn test_frequency_boost_is_log_scale_and_capped() {
544        assert_eq!(frequency_boost(0), 0);
545        assert_eq!(frequency_boost(1), 0);
546        assert_eq!(frequency_boost(2), 4);
547        assert_eq!(frequency_boost(3), 4);
548        assert_eq!(frequency_boost(4), 8);
549        assert_eq!(frequency_boost(7), 8);
550        assert_eq!(frequency_boost(8), 12);
551        assert_eq!(frequency_boost(10_000), 12);
552    }
553
554    #[test]
555    fn test_filter_entries_frequent_command_beats_one_off() {
556        // Same match quality and same recency tier: the command used
557        // repeatedly within the window should outrank the one-off. Without
558        // the frequency boost the tie would break toward index 0.
559        let now = wall_clock_now();
560        let entry = |id: i64, command: &str, use_count: u32| HistoryEntry {
561            id,
562            command: command.to_string(),
563            timestamp: Some(now - 60),
564            working_directory: None,
565            hostname: None,
566            exit_status: None,
567            duration_secs: None,
568            use_count,
569        };
570        let entries = vec![entry(1, "make alpha", 1), entry(2, "make betaa", 8)];
571
572        let mut engine =
573            SearchEngine::new(test_db(), PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
574        let filtered = engine.filter_entries(&entries, &q("make"));
575        assert_eq!(filtered.len(), 2);
576        assert_eq!(
577            entries[filtered[0].0].command, "make betaa",
578            "frequently-used command should outrank the one-off at equal match quality"
579        );
580    }
581
582    #[test]
583    fn test_filter_entries_same_directory_beats_elsewhere() {
584        // Equal match quality, recency tier, and frequency: the command last
585        // run in the engine's working directory should win. Without the
586        // directory boost the tie would break toward index 0.
587        let now = wall_clock_now();
588        let entry = |id: i64, command: &str, dir: &str| HistoryEntry {
589            id,
590            command: command.to_string(),
591            timestamp: Some(now - 60),
592            working_directory: Some(BString::from(dir)),
593            hostname: None,
594            exit_status: None,
595            duration_secs: None,
596            use_count: 1,
597        };
598        let entries = vec![entry(1, "make alpha", "/elsewhere"), entry(2, "make betaa", "/proj")];
599
600        let mut engine =
601            SearchEngine::new(test_db(), PathBuf::from("/proj"), vec![BString::from("host1")], 100);
602        let filtered = engine.filter_entries(&entries, &q("make"));
603        assert_eq!(filtered.len(), 2);
604        assert_eq!(
605            entries[filtered[0].0].command, "make betaa",
606            "same-directory command should outrank one from elsewhere at equal match quality"
607        );
608    }
609
610    #[test]
611    fn test_directory_boost_requires_exact_match() {
612        assert_eq!(directory_boost(Some(b"/proj"), b"/proj"), 8);
613        assert_eq!(directory_boost(Some(b"/proj/sub"), b"/proj"), 0);
614        assert_eq!(directory_boost(Some(b"/other"), b"/proj"), 0);
615        assert_eq!(directory_boost(None, b"/proj"), 0);
616        assert_eq!(directory_boost(Some(b""), b""), 0, "empty cwd must never boost");
617    }
618
619    #[test]
620    fn test_frecency_boost_handles_missing_timestamp() {
621        assert_eq!(frecency_boost(None, 1_000_000), 0);
622    }
623
624    #[test]
625    fn test_frecency_boost_decays_in_steps() {
626        let now = 86_400 * 10_000;
627        let day = 86_400;
628        assert_eq!(frecency_boost(Some(now), now), 24);
629        assert_eq!(frecency_boost(Some(now - 1800), now), 24); // 30 min
630        assert_eq!(frecency_boost(Some(now - 7200), now), 16); // 2 hours
631        assert_eq!(frecency_boost(Some(now - day * 3), now), 10);
632        assert_eq!(frecency_boost(Some(now - day * 14), now), 5);
633        assert_eq!(frecency_boost(Some(now - day * 60), now), 2);
634        assert_eq!(frecency_boost(Some(now - day * 200), now), 0);
635    }
636
637    #[test]
638    fn test_frecency_boost_clamps_future_timestamps() {
639        // Clock skew shouldn't yield a smaller boost than "now".
640        let now = 86_400 * 10_000;
641        assert_eq!(frecency_boost(Some(now + 86_400), now), 24);
642    }
643
644    fn wall_clock_now() -> i64 {
645        crate::unix_now()
646    }
647
648    #[test]
649    fn test_filter_entries_recent_beats_older_better_fuzzy_match() {
650        // For query "p": "python ..." gets nucleo's first-char bonus, while
651        // "cd /p" only gets a word-boundary bonus -- so without frecency the
652        // older python entry would win. With frecency, the fresh /p entry
653        // should outrank it.
654        let conn = test_db();
655        let now = wall_clock_now();
656
657        insert_command(&conn, "python script.py", "host1", "/tmp", now - 86_400 * 200);
658        insert_command(&conn, "cd /p", "host1", "/tmp", now - 60);
659
660        let mut engine =
661            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
662        let entries = engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
663        let filtered = engine.filter_entries(&entries, &q("p"));
664
665        assert!(filtered.len() >= 2);
666        assert_eq!(
667            entries[filtered[0].0].command, "cd /p",
668            "fresh /p should outrank old python under frecency for query 'p'"
669        );
670    }
671
672    #[test]
673    fn test_filter_entries_preserves_fuzzy_quality_when_equally_recent() {
674        // Both entries are within the same frecency tier, so the boost cancels
675        // and the better fuzzy match (first-char "python") should still win.
676        let conn = test_db();
677        let now = wall_clock_now();
678
679        insert_command(&conn, "python script.py", "host1", "/tmp", now - 60);
680        insert_command(&conn, "cd /p", "host1", "/tmp", now - 120);
681
682        let mut engine =
683            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
684        let entries = engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
685        let filtered = engine.filter_entries(&entries, &q("p"));
686
687        assert_eq!(
688            entries[filtered[0].0].command, "python script.py",
689            "with both recent, fuzzy quality (first-char match) should win"
690        );
691    }
692
693    #[test]
694    fn test_global_result_limit_hides_old_commands() {
695        let conn = test_db();
696        let result_limit = 5;
697        // load_entries oversamples by 3x, so we need > result_limit * 3 newer
698        // commands to push the old one beyond the query window.
699        let oversample = result_limit * 3;
700
701        // Insert an old "shutdown" command
702        insert_command(&conn, "sudo shutdown -h now", "host1", "/home/user", 100);
703
704        // Insert more than oversample newer unique commands to push shutdown out
705        for i in 0..(oversample + 1) {
706            insert_command(
707                &conn,
708                &format!("unique-cmd-{i}"),
709                "host1",
710                "/home/user",
711                1000 + i as i64,
712            );
713        }
714
715        let engine = SearchEngine::new(
716            conn,
717            PathBuf::from("/home/user"),
718            vec![BString::from("host1")],
719            result_limit,
720        );
721
722        // Global mode without query: shutdown is beyond the oversample window
723        let entries = engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
724        assert_eq!(entries.len(), oversample);
725        assert!(
726            !entries.iter().any(|e| e.command.contains("shutdown")),
727            "shutdown should be excluded by oversample limit"
728        );
729
730        // Global mode WITH query: LIKE filter narrows before LIMIT, so shutdown is found
731        let entries = engine
732            .load_entries(FilterMode::Global, HostFilter::AllHosts, Some(&q("shutdown")))
733            .unwrap();
734        assert_eq!(entries.len(), 1);
735        assert_eq!(entries[0].command, "sudo shutdown -h now");
736    }
737
738    #[test]
739    fn test_all_hosts_returns_superset_of_this_host() {
740        let conn = test_db();
741        let result_limit = 10;
742
743        // Same commands on both hosts, many with identical timestamps (from sync)
744        for i in 0..8 {
745            insert_command(&conn, &format!("shared-cmd-{i}"), "host1", "/tmp", 1000 + i);
746            insert_command(&conn, &format!("shared-cmd-{i}"), "host2", "/tmp", 1000 + i);
747        }
748        // Commands unique to each host
749        insert_command(&conn, "host1-only", "host1", "/tmp", 900);
750        insert_command(&conn, "host2-only", "host2", "/tmp", 901);
751
752        let engine = SearchEngine::new(
753            conn,
754            PathBuf::from("/tmp"),
755            vec![BString::from("host1")],
756            result_limit,
757        );
758
759        let this_host =
760            engine.load_entries(FilterMode::Global, HostFilter::ThisHost, None).unwrap();
761        let all_hosts =
762            engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
763
764        // AllHosts must return at least as many rows as ThisHost
765        assert!(
766            all_hosts.len() >= this_host.len(),
767            "AllHosts ({}) should have >= ThisHost ({}) entries",
768            all_hosts.len(),
769            this_host.len()
770        );
771
772        // Raw rows (dedup is the caller's job):
773        // AllHosts: 8 commands * 2 hosts + 2 unique = 18
774        // ThisHost: 8 shared + 1 host1-only = 9
775        assert_eq!(all_hosts.len(), 18);
776        assert_eq!(this_host.len(), 9);
777    }
778
779    #[test]
780    fn test_format_relative_time_none() {
781        assert_eq!(format_relative_time(None), "   ");
782    }
783
784    #[test]
785    fn test_format_relative_time_seconds() {
786        let now = crate::unix_now() as u64 as i64;
787        assert_eq!(format_relative_time(Some(now - 30)), "30s");
788        assert_eq!(format_relative_time(Some(now - 5)), " 5s");
789    }
790
791    #[test]
792    fn test_format_relative_time_minutes() {
793        let now = crate::unix_now() as u64 as i64;
794        assert_eq!(format_relative_time(Some(now - 120)), " 2m");
795        assert_eq!(format_relative_time(Some(now - 3000)), "50m");
796    }
797
798    #[test]
799    fn test_format_relative_time_hours() {
800        let now = crate::unix_now() as u64 as i64;
801        assert_eq!(format_relative_time(Some(now - 7200)), " 2h");
802        assert_eq!(format_relative_time(Some(now - 36000)), "10h");
803    }
804
805    #[test]
806    fn test_like_filter_matches_fuzzy_subsequences() {
807        let conn = test_db();
808        insert_command(&conn, "git commit -m 'test'", "host1", "/tmp", 1000);
809        insert_command(&conn, "docker compose up", "host1", "/tmp", 2000);
810        insert_command(&conn, "kubectl get pods", "host1", "/tmp", 3000);
811
812        let engine =
813            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
814
815        // "gcm" should match "git commit -m" via subsequence (g...c...m)
816        let entries =
817            engine.load_entries(FilterMode::Global, HostFilter::AllHosts, Some(&q("gcm"))).unwrap();
818        assert!(
819            entries.iter().any(|e| e.command.contains("git commit")),
820            "fuzzy query 'gcm' should match 'git commit -m', got: {:?}",
821            entries.iter().map(|e| &e.command).collect::<Vec<_>>()
822        );
823
824        // "dcu" should match "docker compose up"
825        let entries =
826            engine.load_entries(FilterMode::Global, HostFilter::AllHosts, Some(&q("dcu"))).unwrap();
827        assert!(
828            entries.iter().any(|e| e.command.contains("docker compose")),
829            "fuzzy query 'dcu' should match 'docker compose up', got: {:?}",
830            entries.iter().map(|e| &e.command).collect::<Vec<_>>()
831        );
832    }
833
834    #[test]
835    fn test_like_filter_atoms_match_any_order() {
836        // nucleo matches whitespace-separated atoms in any order, so the DB
837        // prefilter must too -- one LIKE condition per atom, not one ordered
838        // subsequence for the whole query.
839        let conn = test_db();
840        insert_command(&conn, "git push origin main", "host1", "/tmp", 1000);
841
842        let engine =
843            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
844        let entries = engine
845            .load_entries(FilterMode::Global, HostFilter::AllHosts, Some(&q("push git")))
846            .unwrap();
847        assert_eq!(entries.len(), 1, "reversed word order should still prefilter-match");
848        assert_eq!(entries[0].command, "git push origin main");
849    }
850
851    #[test]
852    fn test_like_filter_strips_fzf_operators() {
853        // nucleo's Pattern::parse understands fzf-style operators; the LIKE
854        // prefilter must strip them rather than demand them as literal chars.
855        let conn = test_db();
856        insert_command(&conn, "cargo build --release", "host1", "/tmp", 1000);
857
858        let engine =
859            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
860
861        for query in ["^cargo", "release$", "'build", "^cargo release$"] {
862            let entries = engine
863                .load_entries(FilterMode::Global, HostFilter::AllHosts, Some(&q(query)))
864                .unwrap();
865            assert_eq!(entries.len(), 1, "query {query:?} should survive the prefilter");
866        }
867    }
868
869    #[test]
870    fn test_like_filter_drops_negated_atoms() {
871        // A negated atom can't be prefiltered (LIKE can only require presence),
872        // so it must be dropped; the fuzzy stage enforces absence. A query of
873        // only negated atoms degrades to the broad (unfiltered) load.
874        let conn = test_db();
875        insert_command(&conn, "vim notes.txt", "host1", "/tmp", 1000);
876        insert_command(&conn, "cargo build", "host1", "/tmp", 2000);
877
878        let engine =
879            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
880
881        let entries = engine
882            .load_entries(FilterMode::Global, HostFilter::AllHosts, Some(&q("!vim")))
883            .unwrap();
884        assert_eq!(entries.len(), 2, "pure-negation query should not prefilter at all");
885
886        let entries = engine
887            .load_entries(FilterMode::Global, HostFilter::AllHosts, Some(&q("!vim cargo")))
888            .unwrap();
889        assert_eq!(entries.len(), 1, "positive atom should still prefilter");
890        assert_eq!(entries[0].command, "cargo build");
891    }
892
893    #[test]
894    fn test_filter_entries_negation_and_prefix() {
895        // End-to-end: the fuzzy stage honors fzf-style operators the prefilter
896        // now lets through.
897        let conn = test_db();
898        insert_command(&conn, "vim notes.txt", "host1", "/tmp", 1000);
899        insert_command(&conn, "cargo build --release", "host1", "/tmp", 2000);
900        insert_command(&conn, "echo cargo", "host1", "/tmp", 3000);
901
902        let mut engine =
903            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
904        let entries = engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
905
906        let filtered = engine.filter_entries(&entries, &q("!vim"));
907        let commands: Vec<_> = filtered.iter().map(|(i, _)| &entries[*i].command).collect();
908        assert!(!commands.iter().any(|c| c.contains("vim")), "negation should exclude vim");
909        assert_eq!(commands.len(), 2);
910
911        let filtered = engine.filter_entries(&entries, &q("^cargo"));
912        assert_eq!(filtered.len(), 1, "^ should anchor to start of command");
913        assert_eq!(entries[filtered[0].0].command, "cargo build --release");
914    }
915
916    #[test]
917    fn test_like_filter_splits_atoms_on_separators() {
918        // Scoring normalizes `-`/`*`/`/` to spaces *before* nucleo splits the
919        // query into atoms, so "rs/main" is two order-independent atoms. The
920        // prefilter must mirror that split or it silently drops rows the
921        // fuzzy stage would match (README promises "src engine" matches
922        // "src/recall/engine.rs" with `/` acting as a word separator).
923        let conn = test_db();
924        insert_command(&conn, "vim src/main.rs", "host1", "/tmp", 1000);
925
926        let engine =
927            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
928        for query in ["rs/main", "rs-main", "rs*main"] {
929            let entries = engine
930                .load_entries(FilterMode::Global, HostFilter::AllHosts, Some(&q(query)))
931                .unwrap();
932            assert_eq!(
933                entries.len(),
934                1,
935                "prefilter must split {query:?} into atoms like the scorer"
936            );
937        }
938    }
939
940    #[test]
941    fn test_prefilter_is_superset_of_fuzzy_stage() {
942        // The core invariant of the two-stage search: every row the fuzzy
943        // stage would accept must survive the DB prefilter. Deterministic
944        // pseudo-random queries over a corpus rich in separators, operators,
945        // and escapes; result_limit is large enough that truncation never
946        // masks a violation.
947        let commands = [
948            "vim src/main.rs",
949            "cargo build --release",
950            "git push origin main",
951            "find . -name '*.rs' -delete",
952            "echo foo bar",
953            "grep -rn limit src/recall/engine.rs",
954            "docker compose up -d",
955            "ls -la /home/user/project",
956        ];
957        let conn = test_db();
958        for (i, cmd) in commands.iter().enumerate() {
959            insert_command(&conn, cmd, "host1", "/tmp", 1000 + i as i64);
960        }
961        let mut engine =
962            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 1000);
963        let all = engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
964
965        // Query fragments deliberately shaped like real typing: separators
966        // inside atoms, fzf operators, escaped spaces, multi-atom queries.
967        let fragments = [
968            "rs/main",
969            "rs-main",
970            "src/rec",
971            "git/push",
972            "build--rel",
973            "foo\\ bar",
974            "^cargo",
975            "'build",
976            "push$",
977            "!vim",
978            "main",
979            "rs",
980            "eng/lim",
981            "up-d",
982            "*.rs",
983            "/home/user",
984            "release",
985            "gpo",
986        ];
987        let mut seed: u64 = 0x9e3779b97f4a7c15;
988        let mut next = move || {
989            seed ^= seed << 13;
990            seed ^= seed >> 7;
991            seed ^= seed << 17;
992            seed
993        };
994        let mut queries: Vec<String> = fragments.iter().map(ToString::to_string).collect();
995        for _ in 0..200 {
996            let a = fragments[(next() % fragments.len() as u64) as usize];
997            let b = fragments[(next() % fragments.len() as u64) as usize];
998            let joiner = [" ", "/", "-"][(next() % 3) as usize];
999            queries.push(format!("{a}{joiner}{b}"));
1000        }
1001
1002        for query in &queries {
1003            let fuzzy_ids: Vec<i64> = engine
1004                .filter_entries(&all, &q(query))
1005                .iter()
1006                .map(|(idx, _)| all[*idx].id)
1007                .collect();
1008            let prefiltered = engine
1009                .load_entries(FilterMode::Global, HostFilter::AllHosts, Some(&q(query)))
1010                .unwrap();
1011            for id in &fuzzy_ids {
1012                assert!(
1013                    prefiltered.iter().any(|e| e.id == *id),
1014                    "query {query:?}: row id {id} ({:?}) passes the fuzzy stage but is dropped by the prefilter",
1015                    all.iter().find(|e| e.id == *id).map(|e| &e.command)
1016                );
1017            }
1018        }
1019    }
1020
1021    #[test]
1022    fn test_like_filter_normalizes_dash_and_star() {
1023        let conn = test_db();
1024        insert_command(&conn, "git log --oneline", "host1", "/tmp", 1000);
1025
1026        let engine =
1027            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
1028
1029        // "git-log" should match "git log" because `-` is normalized to wildcard
1030        let entries = engine
1031            .load_entries(FilterMode::Global, HostFilter::AllHosts, Some(&q("git-log")))
1032            .unwrap();
1033        assert!(
1034            entries.iter().any(|e| e.command.contains("git log")),
1035            "query 'git-log' should match 'git log' (dash normalized), got: {:?}",
1036            entries.iter().map(|e| &e.command).collect::<Vec<_>>()
1037        );
1038    }
1039
1040    #[test]
1041    fn test_delete_entries_by_command_removes_all_duplicates() {
1042        let conn = test_db();
1043        // Insert the same command multiple times (different timestamps simulate real usage)
1044        insert_command(&conn, "git status", "host1", "/tmp", 1000);
1045        insert_command(&conn, "git status", "host1", "/tmp", 2000);
1046        insert_command(&conn, "git status", "host1", "/tmp", 3000);
1047        insert_command(&conn, "other cmd", "host1", "/tmp", 4000);
1048
1049        let engine =
1050            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
1051
1052        let deleted = engine.delete_entries_by_command("git status").unwrap();
1053        assert_eq!(deleted, 3, "should delete all rows matching the command");
1054
1055        let entries = engine.load_entries(FilterMode::Global, HostFilter::AllHosts, None).unwrap();
1056        assert_eq!(entries.len(), 1);
1057        assert_eq!(entries[0].command, "other cmd");
1058    }
1059
1060    #[test]
1061    fn test_format_relative_time_days() {
1062        let now = crate::unix_now() as u64 as i64;
1063        assert_eq!(format_relative_time(Some(now - 86400 * 2)), " 2d");
1064        assert_eq!(format_relative_time(Some(now - 86400 * 5)), " 5d");
1065    }
1066
1067    /// Every recall query must walk `history_start_time` newest-first so
1068    /// `LIMIT` stops after the window, never sort the whole table.
1069    fn assert_window_plan(plan: &[String], label: &str) {
1070        let joined = plan.join(" | ");
1071        assert!(
1072            joined.contains("USING INDEX history_start_time"),
1073            "{label}: expected to walk history_start_time, got: {joined}"
1074        );
1075        assert!(!joined.contains("TEMP B-TREE"), "{label}: plan sorts the whole table: {joined}");
1076    }
1077
1078    #[test]
1079    fn test_recall_query_plans_walk_timestamp_index_without_sort() {
1080        let conn = test_db();
1081        insert_command(&conn, "echo hi", "host1", "/tmp", 1000);
1082        let engine =
1083            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
1084        let query = q("ech");
1085        for mode in [FilterMode::Global, FilterMode::Directory] {
1086            for host in [HostFilter::ThisHost, HostFilter::AllHosts] {
1087                for query in [None, Some(&query)] {
1088                    let (sql, params) = engine.recall_query(mode, host, query);
1089                    let refs: Vec<&dyn rusqlite::types::ToSql> =
1090                        params.iter().map(|p| p as &dyn rusqlite::types::ToSql).collect();
1091                    let plan = crate::test_utils::explain_query_plan(&engine.conn, &sql, &refs);
1092                    assert_window_plan(&plan, &format!("{mode:?}/{host:?}/query={query:?}"));
1093                }
1094            }
1095        }
1096    }
1097
1098    #[test]
1099    fn test_dropping_timestamp_index_forces_full_sort() {
1100        // Proves the assertion above has teeth: without the index SQLite must
1101        // materialise and sort every row before applying LIMIT.
1102        let conn = test_db();
1103        conn.execute_batch("DROP INDEX history_start_time").unwrap();
1104        let engine =
1105            SearchEngine::new(conn, PathBuf::from("/tmp"), vec![BString::from("host1")], 100);
1106        let (sql, params) = engine.recall_query(FilterMode::Global, HostFilter::AllHosts, None);
1107        let refs: Vec<&dyn rusqlite::types::ToSql> =
1108            params.iter().map(|p| p as &dyn rusqlite::types::ToSql).collect();
1109        let plan = crate::test_utils::explain_query_plan(&engine.conn, &sql, &refs).join(" | ");
1110        assert!(plan.contains("TEMP B-TREE"), "expected a sort without the index: {plan}");
1111    }
1112}