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
560
561
562
563
564
565
566
567
568
569
570
571
572
|
//! Search index with frecency-based ranking.
//!
//! This module provides a deduplicated search index where each unique command
//! is stored once, with metadata about all its invocations. This enables:
//!
//! - Efficient fuzzy matching (fewer items to match)
//! - Frecency-based ranking (frequency + recency)
//! - Dynamic filtering by directory, host, session, etc.
use std::{collections::HashMap, sync::Arc};
use atuin_client::history::History;
use dashmap::{DashMap, DashSet};
use nucleo::{Injector, Nucleo, pattern};
use time::OffsetDateTime;
use tokio::sync::RwLock;
use tracing::{Level, instrument};
use crate::components::search::with_trailing_slash;
/// Data for a single invocation of a command.
#[derive(Debug, Clone)]
pub struct Invocation {
/// When the command was run.
pub timestamp: i64,
/// The working directory when the command was run.
#[allow(dead_code)]
pub cwd: String,
/// The hostname where the command was run.
#[allow(dead_code)]
pub hostname: String,
/// The session ID.
#[allow(dead_code)]
pub session: String,
/// The history entry ID (for returning in search results).
pub history_id: String,
}
impl From<&History> for Invocation {
fn from(history: &History) -> Self {
Self {
timestamp: history.timestamp.unix_timestamp(),
cwd: history.cwd.clone(),
hostname: history.hostname.clone(),
session: history.session.clone(),
history_id: history.id.0.clone(),
}
}
}
/// Pre-computed frecency data for O(1) lookup.
#[derive(Debug, Clone, Default)]
pub struct FrecencyData {
/// Total number of times this command was used.
pub count: u32,
/// Most recent usage timestamp (unix seconds).
pub last_used: i64,
}
impl FrecencyData {
/// Record a new usage of this command.
pub fn record_use(&mut self, timestamp: i64) {
self.count += 1;
if timestamp > self.last_used {
self.last_used = timestamp;
}
}
/// Compute frecency score based on count and recency.
///
/// Uses a decay function where more recent commands score higher.
/// The formula balances frequency (how often) with recency (how recent).
#[instrument(level = tracing::Level::TRACE, name = "index_frecency_compute")]
pub fn compute(&self, now: i64) -> u32 {
if self.count == 0 {
return 0;
}
// Time-based decay: score decreases as time passes
let age_seconds = (now - self.last_used).max(0) as u64;
let age_hours = age_seconds / 3600;
// Decay factor: recent commands get higher scores
// - Last hour: multiplier ~1.0
// - Last day: multiplier ~0.5
// - Last week: multiplier ~0.1
// - Older: multiplier approaches 0
let recency_score = match age_hours {
0 => 100,
1..=6 => 90,
7..=24 => 70,
25..=72 => 50,
73..=168 => 30,
169..=720 => 15,
_ => 5,
};
// Frequency boost: more uses = higher score (with diminishing returns)
let frequency_score = ((self.count as f64).ln() * 20.0).min(100.0) as u32;
// Combined score
recency_score + frequency_score
}
}
/// Data for a unique command, including all its invocations.
pub struct CommandData {
/// The command text (stored for debugging/logging purposes).
#[allow(dead_code)]
pub command: String,
/// All invocations of this command, sorted by timestamp (newest first).
pub invocations: Vec<Invocation>,
/// Pre-computed global frecency.
pub global_frecency: FrecencyData,
// Pre-computed indexes for O(1) filter lookups
/// All directories where this command has been run.
directories: DashSet<String>,
/// All hostnames where this command has been run.
hosts: DashSet<String>,
/// All sessions where this command has been run.
sessions: DashSet<String>,
}
impl CommandData {
/// Create a new CommandData from a history entry.
pub fn new(history: &History) -> Self {
let mut data = Self {
command: history.command.clone(),
invocations: Vec::new(),
global_frecency: FrecencyData::default(),
directories: DashSet::new(),
hosts: DashSet::new(),
sessions: DashSet::new(),
};
data.add_invocation(history);
data
}
/// Add an invocation from a history entry.
pub fn add_invocation(&mut self, history: &History) {
let timestamp = history.timestamp.unix_timestamp();
// Update global frecency
self.global_frecency.record_use(timestamp);
// Update pre-computed indexes for O(1) filter lookups
self.directories.insert(with_trailing_slash(&history.cwd));
self.hosts.insert(history.hostname.clone());
self.sessions.insert(history.session.clone());
let invocation = Invocation::from(history);
// Insert sorted by timestamp (newest first)
let pos = self
.invocations
.iter()
.position(|inv| inv.timestamp < timestamp)
.unwrap_or(self.invocations.len());
self.invocations.insert(pos, invocation);
}
/// Get the most recent history ID for this command.
pub fn most_recent_id(&self) -> Option<&str> {
self.invocations.first().map(|inv| inv.history_id.as_str())
}
/// Check if any invocation matches a directory filter (exact match).
/// O(1) lookup using pre-computed index.
pub fn has_invocation_in_dir(&self, dir: &str) -> bool {
self.directories.contains(dir)
}
/// Check if any invocation matches a directory prefix (workspace/git root).
/// O(n) where n = number of unique directories for this command.
pub fn has_invocation_in_workspace(&self, prefix: &str) -> bool {
self.directories.iter().any(|d| d.starts_with(prefix))
}
/// Check if any invocation matches a hostname.
/// O(1) lookup using pre-computed index.
pub fn has_invocation_on_host(&self, hostname: &str) -> bool {
self.hosts.contains(hostname)
}
/// Check if any invocation matches a session.
/// O(1) lookup using pre-computed index.
pub fn has_invocation_in_session(&self, session: &str) -> bool {
self.sessions.contains(session)
}
}
/// Filter mode for search queries.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IndexFilterMode {
/// No filtering - search all commands.
Global,
/// Filter to commands run in a specific directory.
Directory(String),
/// Filter to commands run in a workspace (directory prefix).
Workspace(String),
/// Filter to commands run on a specific host.
Host(String),
/// Filter to commands run in a specific session.
Session(String),
}
/// Context for search queries.
#[derive(Debug, Clone, Default)]
pub struct QueryContext {
pub cwd: Option<String>,
pub git_root: Option<String>,
pub hostname: Option<String>,
pub session_id: Option<String>,
}
/// A deduplicated search index with frecency-based ranking.
///
/// Commands are stored by their text, with metadata about all invocations.
/// Nucleo handles fuzzy matching, while frecency is computed via scorer callback.
///
/// Global frecency is precomputed by a background task and used for scoring.
/// If frecency data is not available, search still works but without frecency ranking;
/// although this should never happen due to precomputing the frecency map.
pub struct SearchIndex {
/// Map from command text to command data.
/// Using DashMap for concurrent read/write access, wrapped in Arc for sharing with scorer.
commands: Arc<DashMap<String, CommandData>>,
/// Nucleo fuzzy matcher - items are command strings.
nucleo: RwLock<Nucleo<String>>,
/// Injector for adding new commands to Nucleo.
injector: Injector<String>,
/// Precomputed global frecency map (command -> frecency score).
/// Updated by background task. If None, search works without frecency.
frecency_map: RwLock<Option<Arc<HashMap<String, u32>>>>,
}
impl SearchIndex {
/// Create a new empty search index.
pub fn new() -> Self {
let nucleo_config = nucleo::Config::DEFAULT;
// Single column for command text
let nucleo = Nucleo::<String>::new(nucleo_config, Arc::new(|| {}), None, 1);
let injector = nucleo.injector();
Self {
commands: Arc::new(DashMap::new()),
nucleo: RwLock::new(nucleo),
injector,
frecency_map: RwLock::new(None),
}
}
/// Add a history entry to the index.
///
/// If the command already exists, updates its invocation data.
/// If it's a new command, adds it to both the map and Nucleo.
pub fn add_history(&self, history: &History) {
let command = &history.command;
if let Some(mut entry) = self.commands.get_mut(command) {
// Existing command - just update invocations
entry.add_invocation(history);
} else {
// New command - add to both map and Nucleo
let data = CommandData::new(history);
self.commands.insert(command.clone(), data);
self.injector.push(command.clone(), |cmd, cols| {
cols[0] = cmd.clone().into();
});
}
// Note: frecency_map is rebuilt by background task, not invalidated here
}
/// Add multiple history entries to the index.
pub fn add_histories(&self, histories: &[History]) {
for history in histories {
self.add_history(history);
}
}
/// Get the number of unique commands in the index.
pub fn command_count(&self) -> usize {
self.commands.len()
}
/// Get the number of items in Nucleo (should match command_count).
pub async fn nucleo_item_count(&self) -> u32 {
self.nucleo.read().await.snapshot().item_count()
}
/// Search for commands matching a query.
///
/// Returns a list of history IDs (most recent invocation per command).
/// Uses precomputed global frecency for scoring if available.
#[instrument(skip_all, level = tracing::Level::TRACE, name = "index_search", fields(query = %query))]
pub async fn search(
&self,
query: &str,
filter_mode: IndexFilterMode,
_context: &QueryContext,
limit: u32,
) -> Vec<String> {
let mut nucleo = self.nucleo.write().await;
// Get precomputed frecency map (may be None if not yet computed)
let frecency_map = self.frecency_map.read().await.clone();
// Build filter based on mode
let filter = self.build_filter(&filter_mode);
nucleo.set_filter(filter);
// Build scorer from precomputed frecency (or None if not available)
let scorer = Self::build_scorer(frecency_map);
nucleo.set_scorer(scorer);
// Update pattern
nucleo.pattern.reparse(
0,
query,
pattern::CaseMatching::Smart,
pattern::Normalization::Smart,
false,
);
tracing::span!(Level::TRACE, "index_search_tick").in_scope(|| {
// Tick until complete
while nucleo.tick(10).running {}
});
// Collect results
let snapshot = nucleo.snapshot();
let matched_count = snapshot.matched_item_count().min(limit);
tracing::span!(Level::TRACE, "index_search_results").in_scope(|| {
snapshot
.matched_items(..matched_count)
.filter_map(|item| {
let cmd = item.data;
self.commands
.get(cmd)
.and_then(|data| data.most_recent_id().map(|s| s.to_string()))
})
.collect()
})
}
/// Rebuild the global frecency map.
///
/// This should be called by a background task periodically.
/// The map is used for scoring search results.
#[instrument(skip_all, level = tracing::Level::DEBUG, name = "rebuild_frecency")]
pub async fn rebuild_frecency(&self) {
let now = OffsetDateTime::now_utc().unix_timestamp();
let mut frecency_map: HashMap<String, u32> = HashMap::new();
for entry in self.commands.iter() {
let frecency = entry.global_frecency.compute(now);
frecency_map.insert(entry.key().clone(), frecency);
}
*self.frecency_map.write().await = Some(Arc::new(frecency_map));
}
/// Build filter predicate for the given mode.
fn build_filter(&self, mode: &IndexFilterMode) -> Option<nucleo::Filter<String>> {
// For Global mode, no filter needed
if matches!(mode, IndexFilterMode::Global) {
return None;
}
// Pre-compute which commands pass the filter
let passing_commands: Arc<std::collections::HashSet<String>> = {
let mut set = std::collections::HashSet::new();
for entry in self.commands.iter() {
let passes = match mode {
IndexFilterMode::Global => unreachable!(),
IndexFilterMode::Directory(dir) => entry.has_invocation_in_dir(dir),
IndexFilterMode::Workspace(prefix) => entry.has_invocation_in_workspace(prefix),
IndexFilterMode::Host(hostname) => entry.has_invocation_on_host(hostname),
IndexFilterMode::Session(session) => entry.has_invocation_in_session(session),
};
if passes {
set.insert(entry.key().clone());
}
}
Arc::new(set)
};
Some(Arc::new(move |cmd: &String| passing_commands.contains(cmd)))
}
/// Build scorer from precomputed frecency map.
///
/// Returns None if frecency map is not available (search still works, just without frecency ranking).
fn build_scorer(
frecency_map: Option<Arc<HashMap<String, u32>>>,
) -> Option<nucleo::Scorer<String>> {
let map = frecency_map?;
Some(Arc::new(move |cmd: &String, fuzzy_score: u32| {
let frecency = map.get(cmd).copied().unwrap_or(0);
fuzzy_score + frecency
}))
}
}
impl Default for SearchIndex {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use time::macros::datetime;
fn make_history(command: &str, cwd: &str, timestamp: OffsetDateTime) -> History {
History::import()
.timestamp(timestamp)
.command(command)
.cwd(cwd)
.build()
.into()
}
#[test]
fn frecency_data_compute() {
let now = 1000000i64;
// Recent command
let recent = FrecencyData {
count: 5,
last_used: now - 60, // 1 minute ago
};
assert!(recent.compute(now) > 100); // High score
// Old command
let old = FrecencyData {
count: 5,
last_used: now - 86400 * 30, // 30 days ago
};
assert!(old.compute(now) < recent.compute(now));
// Frequently used old command
let frequent_old = FrecencyData {
count: 100,
last_used: now - 86400 * 7, // 1 week ago
};
// Should still have decent score due to frequency
assert!(frequent_old.compute(now) > 50);
}
#[test]
fn command_data_add_invocation() {
let (dir1, dir2) = if cfg!(windows) {
("C:\\Users\\User\\project", "C:\\Users\\User\\other")
} else {
("/home/user/project", "/home/user/other")
};
let history1 = make_history("git status", dir1, datetime!(2024-01-01 10:00 UTC));
let history2 = make_history("git status", dir2, datetime!(2024-01-01 12:00 UTC));
let mut data = CommandData::new(&history1);
assert_eq!(data.invocations.len(), 1);
assert_eq!(data.global_frecency.count, 1);
data.add_invocation(&history2);
assert_eq!(data.invocations.len(), 2);
assert_eq!(data.global_frecency.count, 2);
// Most recent should be first
assert_eq!(data.invocations[0].cwd, dir2);
assert_eq!(data.invocations[1].cwd, dir1);
}
#[test]
fn command_data_filters() {
let (dir1, dir2) = if cfg!(windows) {
("C:\\Users\\User\\project", "C:\\Users\\User\\other")
} else {
("/home/user/project", "/home/user/other")
};
let h1 = make_history("git status", dir1, datetime!(2024-01-01 10:00 UTC));
let h2 = make_history("git status", dir2, datetime!(2024-01-01 12:00 UTC));
let mut data = CommandData::new(&h1);
data.add_invocation(&h2);
let (check1, check2, check3) = if cfg!(windows) {
(
with_trailing_slash("C:\\Users\\User\\project"),
with_trailing_slash("C:\\Users\\User\\other"),
with_trailing_slash("C:\\Users\\User\\missing"),
)
} else {
(
with_trailing_slash("/home/user/project"),
with_trailing_slash("/home/user/other"),
with_trailing_slash("/home/user/missing"),
)
};
assert!(data.has_invocation_in_dir(&check1));
assert!(data.has_invocation_in_dir(&check2));
assert!(!data.has_invocation_in_dir(&check3));
let (check1, check2, check3) = if cfg!(windows) {
(
with_trailing_slash("C:\\Users\\User"),
with_trailing_slash("C:\\Users"),
with_trailing_slash("C:\\Users\\User\\var"),
)
} else {
(
with_trailing_slash("/home/user"),
with_trailing_slash("/home"),
with_trailing_slash("/var"),
)
};
assert!(data.has_invocation_in_workspace(&check1));
assert!(data.has_invocation_in_workspace(&check2));
assert!(!data.has_invocation_in_workspace(&check3));
}
#[tokio::test]
async fn search_index_add_and_search() {
let index = SearchIndex::new();
let h1 = make_history(
"git status",
"/home/user/project",
datetime!(2024-01-01 10:00 UTC),
);
let h2 = make_history(
"git commit -m 'test'",
"/home/user/project",
datetime!(2024-01-01 10:05 UTC),
);
let h3 = make_history(
"ls -la",
"/home/user/other",
datetime!(2024-01-01 10:10 UTC),
);
index.add_history(&h1);
index.add_history(&h2);
index.add_history(&h3);
assert_eq!(index.command_count(), 3);
// Search for "git" - should match 2 commands
let results = index
.search("git", IndexFilterMode::Global, &QueryContext::default(), 10)
.await;
assert_eq!(results.len(), 2);
// Search with directory filter
let results = index
.search(
"",
IndexFilterMode::Directory(with_trailing_slash("/home/user/project")),
&QueryContext::default(),
10,
)
.await;
assert_eq!(results.len(), 2); // git status and git commit
}
}
|