zshrs 0.11.40

The first compiled Unix shell — bytecode VM, worker pool, AOP intercept, Rkyv caching
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
//! SQLite-backed completion engine (legacy / unused).
//!
//! Vestigial. The live completion hot path is the rkyv-mmap'd shard
//! set (see `src/extensions/autoload_cache.rs` and `src/compsys/`);
//! this module's `CompletionEngine` has zero callers. Kept only so
//! removing it stays a deliberate cleanup commit rather than silent
//! drift. Treat the rest of this file as a historical artifact —
//! it does not back any completion behavior in the running shell.

use rusqlite::{params, Connection};
use std::path::PathBuf;
/// `CompletionEngine` — see fields for layout.
pub struct CompletionEngine {
    /// `conn` field.
    conn: Connection,
}
/// `Completion` — see fields for layout.
#[derive(Debug, Clone)]
pub struct Completion {
    /// `name` field.
    pub name: String,
    /// `kind` field.
    pub kind: CompletionKind,
    /// `description` field.
    pub description: Option<String>,
    /// `frequency` field.
    pub frequency: u32,
}
/// `CompletionKind` — see variants.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompletionKind {
    /// `Command` variant.
    Command,
    /// `Builtin` variant.
    Builtin,
    /// `Function` variant.
    Function,
    /// `Alias` variant.
    Alias,
    /// `File` variant.
    File,
    /// `Directory` variant.
    Directory,
    /// `Variable` variant.
    Variable,
    /// `Option` variant.
    Option,
}

impl CompletionKind {
    /// `as_str` — see implementation.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Command => "command",
            Self::Builtin => "builtin",
            Self::Function => "function",
            Self::Alias => "alias",
            Self::File => "file",
            Self::Directory => "directory",
            Self::Variable => "variable",
            Self::Option => "option",
        }
    }

    fn from_str(s: &str) -> Self {
        match s {
            "command" => Self::Command,
            "builtin" => Self::Builtin,
            "function" => Self::Function,
            "alias" => Self::Alias,
            "file" => Self::File,
            "directory" => Self::Directory,
            "variable" => Self::Variable,
            "option" => Self::Option,
            _ => Self::Command,
        }
    }
}

impl CompletionEngine {
    /// `new` — see implementation.
    pub fn new() -> rusqlite::Result<Self> {
        let db_path = Self::db_path();
        std::fs::create_dir_all(db_path.parent().unwrap()).ok();
        let conn = Connection::open(&db_path)?;

        let engine = Self { conn };
        engine.init_schema()?;
        Ok(engine)
    }
    /// `in_memory` — see implementation.
    pub fn in_memory() -> rusqlite::Result<Self> {
        let conn = Connection::open_in_memory()?;
        let engine = Self { conn };
        engine.init_schema()?;
        Ok(engine)
    }

    fn db_path() -> PathBuf {
        // All zshrs state lives under `$ZSHRS_HOME` (default `~/.zshrs`),
        // matching the daemon's CachePaths convention (daemon/paths.rs)
        // and the `~/.zinit`/`~/.zpwr`/`~/.oh-my-zsh` precedent. We
        // explicitly DO NOT use `dirs::cache_dir()` because that
        // resolves to `~/Library/Caches/zshrs` on macOS or
        // `~/.cache/zshrs` on Linux — both forbidden per project policy.
        let root = if let Some(custom) = std::env::var_os("ZSHRS_HOME") {
            PathBuf::from(custom)
        } else {
            dirs::home_dir()
                .unwrap_or_else(|| PathBuf::from("."))
                .join(".zshrs")
        };
        root.join("completions.db")
    }

    fn init_schema(&self) -> rusqlite::Result<()> {
        self.conn.execute_batch(
            r#"
            CREATE TABLE IF NOT EXISTS completions (
                id INTEGER PRIMARY KEY,
                name TEXT NOT NULL,
                kind TEXT NOT NULL,
                description TEXT,
                frequency INTEGER DEFAULT 0,
                UNIQUE(name, kind)
            );

            CREATE VIRTUAL TABLE IF NOT EXISTS completions_fts USING fts5(
                name,
                description,
                content='completions',
                content_rowid='id'
            );

            CREATE TRIGGER IF NOT EXISTS completions_ai AFTER INSERT ON completions BEGIN
                INSERT INTO completions_fts(rowid, name, description)
                VALUES (new.id, new.name, new.description);
            END;

            CREATE TRIGGER IF NOT EXISTS completions_ad AFTER DELETE ON completions BEGIN
                INSERT INTO completions_fts(completions_fts, rowid, name, description)
                VALUES ('delete', old.id, old.name, old.description);
            END;

            CREATE TRIGGER IF NOT EXISTS completions_au AFTER UPDATE ON completions BEGIN
                INSERT INTO completions_fts(completions_fts, rowid, name, description)
                VALUES ('delete', old.id, old.name, old.description);
                INSERT INTO completions_fts(rowid, name, description)
                VALUES (new.id, new.name, new.description);
            END;

            CREATE INDEX IF NOT EXISTS idx_completions_name ON completions(name);
            CREATE INDEX IF NOT EXISTS idx_completions_kind ON completions(kind);
            CREATE INDEX IF NOT EXISTS idx_completions_frequency ON completions(frequency DESC);
        "#,
        )?;
        Ok(())
    }
    /// `add_completion` — see implementation.
    pub fn add_completion(
        &self,
        name: &str,
        kind: CompletionKind,
        description: Option<&str>,
    ) -> rusqlite::Result<()> {
        self.conn.execute(
            "INSERT OR IGNORE INTO completions (name, kind, description) VALUES (?1, ?2, ?3)",
            params![name, kind.as_str(), description],
        )?;
        Ok(())
    }
    /// `add_completions` — see implementation.
    pub fn add_completions(
        &self,
        completions: &[(String, CompletionKind, Option<String>)],
    ) -> rusqlite::Result<()> {
        let tx = self.conn.unchecked_transaction()?;
        {
            let mut stmt = self.conn.prepare(
                "INSERT OR IGNORE INTO completions (name, kind, description) VALUES (?1, ?2, ?3)",
            )?;
            for (name, kind, desc) in completions {
                stmt.execute(params![name, kind.as_str(), desc.as_deref()])?;
            }
        }
        tx.commit()?;
        Ok(())
    }
    /// `increment_frequency` — see implementation.
    pub fn increment_frequency(&self, name: &str) -> rusqlite::Result<()> {
        self.conn.execute(
            "UPDATE completions SET frequency = frequency + 1 WHERE name = ?1",
            params![name],
        )?;
        Ok(())
    }
    /// `search` — see implementation.
    pub fn search(&self, query: &str, limit: usize) -> rusqlite::Result<Vec<Completion>> {
        if query.is_empty() {
            return self.get_top_by_frequency(limit);
        }

        // Try prefix match first (faster)
        let prefix_results = self.search_prefix(query, limit)?;
        if prefix_results.len() >= limit {
            return Ok(prefix_results);
        }

        // Fall back to FTS5 fuzzy search
        self.search_fts(query, limit)
    }

    fn search_prefix(&self, prefix: &str, limit: usize) -> rusqlite::Result<Vec<Completion>> {
        let mut stmt = self.conn.prepare(
            "SELECT name, kind, description, frequency FROM completions 
             WHERE name LIKE ?1 || '%'
             ORDER BY frequency DESC, name ASC
             LIMIT ?2",
        )?;

        let rows = stmt.query_map(params![prefix, limit as i64], |row| {
            Ok(Completion {
                name: row.get(0)?,
                kind: CompletionKind::from_str(&row.get::<_, String>(1)?),
                description: row.get(2)?,
                frequency: row.get(3)?,
            })
        })?;

        rows.collect()
    }

    fn search_fts(&self, query: &str, limit: usize) -> rusqlite::Result<Vec<Completion>> {
        let fts_query = format!("{}*", query);
        let mut stmt = self.conn.prepare(
            "SELECT c.name, c.kind, c.description, c.frequency 
             FROM completions c
             JOIN completions_fts fts ON c.id = fts.rowid
             WHERE completions_fts MATCH ?1
             ORDER BY c.frequency DESC, rank
             LIMIT ?2",
        )?;

        let rows = stmt.query_map(params![fts_query, limit as i64], |row| {
            Ok(Completion {
                name: row.get(0)?,
                kind: CompletionKind::from_str(&row.get::<_, String>(1)?),
                description: row.get(2)?,
                frequency: row.get(3)?,
            })
        })?;

        rows.collect()
    }

    fn get_top_by_frequency(&self, limit: usize) -> rusqlite::Result<Vec<Completion>> {
        let mut stmt = self.conn.prepare(
            "SELECT name, kind, description, frequency FROM completions 
             ORDER BY frequency DESC, name ASC
             LIMIT ?1",
        )?;

        let rows = stmt.query_map(params![limit as i64], |row| {
            Ok(Completion {
                name: row.get(0)?,
                kind: CompletionKind::from_str(&row.get::<_, String>(1)?),
                description: row.get(2)?,
                frequency: row.get(3)?,
            })
        })?;

        rows.collect()
    }
    /// `count` — see implementation.
    pub fn count(&self) -> rusqlite::Result<usize> {
        self.conn
            .query_row("SELECT COUNT(*) FROM completions", [], |row| row.get(0))
    }
    /// `index_system_commands` — see implementation.
    pub fn index_system_commands(&self) -> rusqlite::Result<usize> {
        let path = std::env::var("PATH").unwrap_or_default();
        let mut completions = Vec::new();

        for dir in path.split(':') {
            if let Ok(entries) = std::fs::read_dir(dir) {
                for entry in entries.flatten() {
                    if let Ok(ft) = entry.file_type() {
                        if ft.is_file() || ft.is_symlink() {
                            if let Some(name) = entry.file_name().to_str() {
                                completions.push((name.to_string(), CompletionKind::Command, None));
                            }
                        }
                    }
                }
            }
        }

        let count = completions.len();
        self.add_completions(&completions)?;
        Ok(count)
    }
    /// `index_shell_builtins` — see implementation.
    pub fn index_shell_builtins(&self) -> rusqlite::Result<usize> {
        let builtins = [
            ("cd", "Change directory"),
            ("pwd", "Print working directory"),
            ("echo", "Print arguments"),
            ("export", "Set environment variable"),
            ("unset", "Unset environment variable"),
            ("alias", "Define alias"),
            ("unalias", "Remove alias"),
            ("source", "Execute file in current shell"),
            ("exit", "Exit the shell"),
            ("jobs", "List background jobs"),
            ("fg", "Bring job to foreground"),
            ("bg", "Continue job in background"),
            ("history", "Show command history"),
            ("set", "Set shell options"),
            ("unset", "Unset shell options"),
            ("type", "Show command type"),
            ("which", "Show command path"),
            ("builtin", "Execute builtin command"),
            ("command", "Execute external command"),
            ("exec", "Replace shell with command"),
            ("eval", "Evaluate arguments as command"),
            ("read", "Read input"),
            ("printf", "Formatted print"),
            ("test", "Evaluate conditional expression"),
            ("true", "Return success"),
            ("false", "Return failure"),
            (":", "Null command"),
            ("return", "Return from function"),
            ("break", "Break from loop"),
            ("continue", "Continue loop"),
            ("shift", "Shift positional parameters"),
            ("wait", "Wait for background jobs"),
            ("trap", "Set signal handler"),
            ("umask", "Set file creation mask"),
            ("ulimit", "Set resource limits"),
            ("times", "Show shell times"),
            ("kill", "Send signal to process"),
            ("let", "Evaluate arithmetic expression"),
            ("declare", "Declare variable"),
            ("local", "Declare local variable"),
            ("readonly", "Make variable readonly"),
            ("typeset", "Declare variable type"),
            ("hash", "Remember command path"),
            ("dirs", "Show directory stack"),
            ("pushd", "Push directory"),
            ("popd", "Pop directory"),
            ("getopts", "Parse options"),
            ("enable", "Enable/disable builtins"),
            ("logout", "Exit login shell"),
            ("suspend", "Suspend shell"),
            ("disown", "Remove job from table"),
        ];

        let completions: Vec<_> = builtins
            .iter()
            .map(|(name, desc)| {
                (
                    name.to_string(),
                    CompletionKind::Builtin,
                    Some(desc.to_string()),
                )
            })
            .collect();

        let count = completions.len();
        self.add_completions(&completions)?;
        Ok(count)
    }
}

impl Default for CompletionEngine {
    fn default() -> Self {
        Self::new().expect("Failed to create completion engine")
    }
}

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

    #[test]
    fn test_completion_engine() {
        let _g = crate::test_util::global_state_lock();
        let engine = CompletionEngine::in_memory().unwrap();

        engine
            .add_completion("git", CompletionKind::Command, Some("Version control"))
            .unwrap();
        engine
            .add_completion("grep", CompletionKind::Command, Some("Search text"))
            .unwrap();
        engine
            .add_completion("gzip", CompletionKind::Command, Some("Compress files"))
            .unwrap();

        let results = engine.search("g", 10).unwrap();
        assert_eq!(results.len(), 3);

        let results = engine.search("gi", 10).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "git");
    }

    #[test]
    fn test_frequency_ranking() {
        let _g = crate::test_util::global_state_lock();
        let engine = CompletionEngine::in_memory().unwrap();

        engine
            .add_completion("aaa", CompletionKind::Command, None)
            .unwrap();
        engine
            .add_completion("aab", CompletionKind::Command, None)
            .unwrap();

        // Increment aab frequency
        for _ in 0..5 {
            engine.increment_frequency("aab").unwrap();
        }

        let results = engine.search("aa", 10).unwrap();
        assert_eq!(results[0].name, "aab"); // Higher frequency first
    }
}