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
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
//! zshrs configuration file — `~/.config/zshrs/config.toml`.
//!
//! **zshrs-original infrastructure — no C source counterpart.** C
//! zsh has no equivalent because every runtime knob lives in shell
//! options (Src/options.c) or special parameters
//! (Src/params.c). This file controls the Rust engine — worker-pool
//! size, completion-cache enablement, async-history writes — none
//! of which exist in C zsh.
//!
//! Runtime settings that don't belong in .zshrc (shell script).
//! These control the Rust engine, not the shell language.
//!
//! Example config:
//! ```toml
//! [worker_pool]
//! size = 8            # number of worker threads (default: num_cpus, clamped [2, 18])
//!
//! [completion]
//! max_matches = 1000  # max completion results to display
//! fts_enabled = true  # populate the SQLite FTS5 mirror tables (rkyv shards are the authoritative completion cache; this toggle only affects `dbview` / SQL inspection)
//! ast_cache = true    # pre-parse autoload functions to AST blobs
//!
//! [compsys]
//! backend = "rust"    # "rust" → src/compsys/ported/ ports;
//!                     # "shell" → upstream Completion/ shell funcs
//!                     # via autoload (use this if you've patched
//!                     # any _X in .zshrc).
//!
//! [history]
//! async_writes = true # write history on worker pool (don't block prompt)
//! max_entries = 100000
//!
//! [glob]
//! parallel_threshold = 32  # min files before parallel metadata prefetch
//! recursive_parallel = true  # fan out **/ across worker pool
//!
//! [log]
//! level = "info"      # trace, debug, info, warn, error
//! ```

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

/// Top-level config.
/// zshrs-original — no C counterpart. Each section maps onto a
/// Rust subsystem that doesn't exist in C zsh (worker pool,
/// rkyv-mmap'd completion cache with optional SQLite FTS5
/// mirrors for `dbview`, async history writes, parallel
/// glob).
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
#[derive(Default)]
pub struct ZshrsConfig {
    /// `worker_pool` field.
    pub worker_pool: WorkerPoolConfig,
    /// `completion` field.
    pub completion: CompletionConfig,
    /// `compsys` field — backend selector (rust vs shell) for the
    /// `_main_complete` function tree. See [`CompsysConfig`].
    pub compsys: CompsysConfig,
    /// `history` field.
    pub history: HistoryConfig,
    /// `glob` field.
    pub glob: GlobConfig,
    /// `log` field.
    pub log: LogConfig,
}
/// Compsys backend selection — Rust port vs upstream shell functions.
///
/// `_main_complete` and the rest of the compsys function tree
/// (`Completion/Base/Core/*`, `Zsh/Type/*`, `Zsh/Command/*`, ...)
/// exist in two parallel forms in this repo:
///
/// 1. **Rust ports** under `src/compsys/ported/` — JIT-fast, no
///    fork-exec, deterministic. Used when `backend = "rust"`.
/// 2. **Upstream shell sources** under `src/zsh/Completion/` —
///    autoloaded via `fpath` exactly like real zsh. Used when
///    `backend = "shell"`. Required if you've patched a `_X`
///    function in `.zshrc` and need the user override to win.
///
/// Default `"rust"`. Override per-machine in `~/.config/zshrs/config.toml`:
/// ```toml
/// [compsys]
/// backend = "shell"
/// ```
#[derive(Debug, Clone, Deserialize, Default)]
#[serde(default)]
pub struct CompsysConfig {
    /// Either `"rust"` (default) or `"shell"`. Any other value falls
    /// back to `"rust"` with a one-shot tracing::warn at load time.
    pub backend: CompsysBackend,
}

/// Strong-typed backend selector. Maps to the `backend = "..."`
/// string in the TOML.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum CompsysBackend {
    /// Route every `_NAME` call through `src/compsys/ported/`.
    #[default]
    Rust,
    /// Route every `_NAME` call through the upstream shell function
    /// at `Completion/.../$NAME` via the standard shfunc/autoload path.
    Shell,
}

/// `WorkerPoolConfig` — see fields for layout.
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
#[derive(Default)]
pub struct WorkerPoolConfig {
    /// Number of worker threads. 0 = auto (num_cpus clamped [2, 18]).
    pub size: usize,
}
/// `CompletionConfig` — see fields for layout.
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct CompletionConfig {
    /// `max_matches` field.
    pub max_matches: usize,
    /// `fts_enabled` field.
    pub fts_enabled: bool,
    /// `ast_cache` field.
    pub ast_cache: bool,
}
/// `HistoryConfig` — see fields for layout.
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct HistoryConfig {
    /// `async_writes` field.
    pub async_writes: bool,
    /// `max_entries` field.
    pub max_entries: usize,
}
/// `GlobConfig` — see fields for layout.
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct GlobConfig {
    /// Minimum file count before parallel metadata prefetch kicks in.
    pub parallel_threshold: usize,
    /// Fan out **/ recursive globs across worker pool.
    pub recursive_parallel: bool,
}
/// `LogConfig` — see fields for layout.
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct LogConfig {
    /// `level` field.
    pub level: String,
}

// ── Defaults ──

impl Default for CompletionConfig {
    fn default() -> Self {
        Self {
            max_matches: 1000,
            fts_enabled: true,
            ast_cache: true,
        }
    }
}

impl Default for HistoryConfig {
    fn default() -> Self {
        Self {
            async_writes: true,
            max_entries: 100_000,
        }
    }
}

impl Default for GlobConfig {
    fn default() -> Self {
        Self {
            parallel_threshold: 32,
            recursive_parallel: true,
        }
    }
}

impl Default for LogConfig {
    fn default() -> Self {
        Self {
            level: "info".to_string(),
        }
    }
}

// ── Loading ──

/// Config file path: `$ZSHRS_HOME/zshrs.toml` or
/// `~/.zshrs/zshrs.toml`. Single file for the whole zshrs config
/// surface — shares the path with `daemon_presence::load_zshrs_toml`
/// (which parses the orthogonal `[log]/[daemon]/[shell]/[builtins]`
/// sections). Unknown sections are ignored by serde (`#[serde(default)]`
/// on every field), so the two loaders coexist in one file.
/// zshrs-original — C zsh has no analog.
pub fn config_path() -> PathBuf {
    crate::daemon_presence::config_file_path().unwrap_or_else(|| PathBuf::from("/tmp/zshrs.toml"))
}

/// Load config from disk. Returns defaults if the file doesn't
/// exist or fails to parse.
/// zshrs-original — no C counterpart.
pub fn load() -> ZshrsConfig {
    load_from(&config_path())
}

/// Process-cached config snapshot. Read once on first access; the
/// disk file is NOT re-read on each call (no reload-on-write). Hot
/// paths like `dispatch_function_call` consult this without
/// stat'ing / re-parsing the TOML.
pub fn current() -> &'static ZshrsConfig {
    static CACHED: std::sync::OnceLock<ZshrsConfig> = std::sync::OnceLock::new();
    CACHED.get_or_init(load)
}

/// Load config from a specific path.
/// zshrs-original — no C counterpart. Defaults preserve startup
/// silently (per the project's "no startup chatter" rule).
pub fn load_from(path: &Path) -> ZshrsConfig {
    match std::fs::read_to_string(path) {
        Ok(content) => match toml::from_str(&content) {
            Ok(config) => {
                tracing::info!(path = %path.display(), "config loaded");
                config
            }
            Err(e) => {
                tracing::warn!(
                    path = %path.display(),
                    error = %e,
                    "config parse error, using defaults"
                );
                ZshrsConfig::default()
            }
        },
        Err(_) => {
            // No config file — use defaults silently
            ZshrsConfig::default()
        }
    }
}

/// Resolve the worker pool size from config.
/// `0` means auto: `available_parallelism` clamped to `[2, 18]`.
/// zshrs-original — sizes the thread pool (`src/worker.rs`) that
/// replaces C zsh's per-task `fork(2)` strategy.
pub fn resolve_pool_size(config: &WorkerPoolConfig) -> usize {
    if config.size > 0 {
        config.size.clamp(1, 64)
    } else {
        std::thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(4)
            .clamp(2, 18)
    }
}

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

    #[test]
    fn test_default_config() {
        let _g = crate::test_util::global_state_lock();
        let config = ZshrsConfig::default();
        assert_eq!(config.worker_pool.size, 0);
        assert_eq!(config.completion.max_matches, 1000);
        assert!(config.completion.fts_enabled);
        assert!(config.completion.ast_cache);
        assert!(config.history.async_writes);
        assert!(config.glob.recursive_parallel);
        assert_eq!(config.glob.parallel_threshold, 32);
    }

    #[test]
    fn test_parse_toml() {
        let _g = crate::test_util::global_state_lock();
        let toml = r#"
[worker_pool]
size = 4

[completion]
max_matches = 500
ast_cache = false

[glob]
parallel_threshold = 64
"#;
        let config: ZshrsConfig = toml::from_str(toml).unwrap();
        assert_eq!(config.worker_pool.size, 4);
        assert_eq!(config.completion.max_matches, 500);
        assert!(!config.completion.ast_cache);
        assert_eq!(config.glob.parallel_threshold, 64);
        // Unset fields use defaults
        assert!(config.history.async_writes);
        assert!(config.glob.recursive_parallel);
    }

    #[test]
    fn test_resolve_pool_size() {
        let _g = crate::test_util::global_state_lock();
        let auto = WorkerPoolConfig { size: 0 };
        let resolved = resolve_pool_size(&auto);
        assert!((2..=18).contains(&resolved));

        let explicit = WorkerPoolConfig { size: 4 };
        assert_eq!(resolve_pool_size(&explicit), 4);

        let clamped = WorkerPoolConfig { size: 999 };
        assert_eq!(resolve_pool_size(&clamped), 64);
    }

    #[test]
    fn test_missing_file_returns_defaults() {
        let _g = crate::test_util::global_state_lock();
        let config = load_from(Path::new("/nonexistent/config.toml"));
        assert_eq!(config.worker_pool.size, 0);
    }

    // ========================================================
    // resolve_pool_size — boundary behavior
    // ========================================================

    #[test]
    fn pool_size_one_passes_through() {
        // Explicit size=1 should NOT trigger the auto-detect branch.
        let _g = crate::test_util::global_state_lock();
        assert_eq!(resolve_pool_size(&WorkerPoolConfig { size: 1 }), 1);
    }

    #[test]
    fn pool_size_64_is_upper_cap_for_explicit_request() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(resolve_pool_size(&WorkerPoolConfig { size: 64 }), 64);
        assert_eq!(resolve_pool_size(&WorkerPoolConfig { size: 65 }), 64);
        assert_eq!(resolve_pool_size(&WorkerPoolConfig { size: 100_000 }), 64);
    }

    #[test]
    fn pool_size_zero_uses_auto_within_2_to_18_window() {
        let _g = crate::test_util::global_state_lock();
        let n = resolve_pool_size(&WorkerPoolConfig { size: 0 });
        assert!(
            (2..=18).contains(&n),
            "auto-detect must clamp to [2,18], got {}",
            n
        );
    }

    // ========================================================
    // Parser — defaults round-trip on empty input
    // ========================================================

    #[test]
    fn empty_toml_parses_to_full_defaults() {
        let _g = crate::test_util::global_state_lock();
        let cfg: ZshrsConfig = toml::from_str("").unwrap();
        let d = ZshrsConfig::default();
        assert_eq!(cfg.worker_pool.size, d.worker_pool.size);
        assert_eq!(cfg.completion.max_matches, d.completion.max_matches);
        assert_eq!(cfg.compsys.backend, d.compsys.backend);
        assert_eq!(cfg.history.max_entries, d.history.max_entries);
        assert_eq!(cfg.glob.parallel_threshold, d.glob.parallel_threshold);
        assert_eq!(cfg.log.level, d.log.level);
    }

    #[test]
    fn compsys_backend_defaults_to_rust() {
        let _g = crate::test_util::global_state_lock();
        let cfg = ZshrsConfig::default();
        assert_eq!(cfg.compsys.backend, CompsysBackend::Rust);
    }

    #[test]
    fn compsys_backend_parses_explicit_shell() {
        let _g = crate::test_util::global_state_lock();
        let cfg: ZshrsConfig = toml::from_str("[compsys]\nbackend = \"shell\"\n").unwrap();
        assert_eq!(cfg.compsys.backend, CompsysBackend::Shell);
    }

    #[test]
    fn compsys_backend_parses_explicit_rust() {
        let _g = crate::test_util::global_state_lock();
        let cfg: ZshrsConfig = toml::from_str("[compsys]\nbackend = \"rust\"\n").unwrap();
        assert_eq!(cfg.compsys.backend, CompsysBackend::Rust);
    }

    #[test]
    fn unknown_section_does_not_error_with_serde_default() {
        let _g = crate::test_util::global_state_lock();
        // Unknown top-level key is currently rejected when serde sees it
        // — verify the explicit accepted shape stays accepted.
        let toml = "[log]\nlevel = \"debug\"\n";
        let cfg: ZshrsConfig = toml::from_str(toml).unwrap();
        assert_eq!(cfg.log.level, "debug");
    }

    #[test]
    fn partial_completion_section_fills_other_defaults() {
        let _g = crate::test_util::global_state_lock();
        let toml = r#"
[completion]
max_matches = 42
"#;
        let cfg: ZshrsConfig = toml::from_str(toml).unwrap();
        assert_eq!(cfg.completion.max_matches, 42);
        // Other fields fall back to defaults.
        assert!(cfg.completion.fts_enabled);
        assert!(cfg.completion.ast_cache);
    }

    #[test]
    fn history_async_can_be_disabled() {
        let _g = crate::test_util::global_state_lock();
        let toml = "[history]\nasync_writes = false\n";
        let cfg: ZshrsConfig = toml::from_str(toml).unwrap();
        assert!(!cfg.history.async_writes);
        assert_eq!(cfg.history.max_entries, 100_000);
    }

    #[test]
    fn glob_recursive_parallel_can_be_disabled() {
        let _g = crate::test_util::global_state_lock();
        let toml = "[glob]\nrecursive_parallel = false\n";
        let cfg: ZshrsConfig = toml::from_str(toml).unwrap();
        assert!(!cfg.glob.recursive_parallel);
    }

    // ========================================================
    // load_from — IO-failure modes
    // ========================================================

    #[test]
    fn malformed_toml_returns_defaults_not_panic() {
        // Write a bogus file then point load_from at it.
        let _g = crate::test_util::global_state_lock();
        let tmp = std::env::temp_dir().join("zshrs_config_malformed.toml");
        std::fs::write(&tmp, "this is not [[ valid toml ===").unwrap();
        let cfg = load_from(&tmp);
        // Defaults survive parse error.
        assert_eq!(cfg.worker_pool.size, 0);
        assert_eq!(cfg.completion.max_matches, 1000);
        let _ = std::fs::remove_file(&tmp);
    }

    #[test]
    fn load_from_round_trip_via_temp_file() {
        let _g = crate::test_util::global_state_lock();
        let tmp = std::env::temp_dir().join("zshrs_config_rt.toml");
        std::fs::write(&tmp, "[worker_pool]\nsize = 7\n").unwrap();
        let cfg = load_from(&tmp);
        assert_eq!(cfg.worker_pool.size, 7);
        assert_eq!(resolve_pool_size(&cfg.worker_pool), 7);
        let _ = std::fs::remove_file(&tmp);
    }

    #[test]
    fn config_path_ends_in_config_toml() {
        let _g = crate::test_util::global_state_lock();
        let p = config_path();
        // Canonical name is `zshrs.toml` per
        // daemon_presence::config_file_path (line 399) — documented at
        // daemon_presence.rs:30 as `$ZSHRS_HOME/zshrs.toml` or
        // `~/.zshrs/zshrs.toml`. Test name says "config_toml" but
        // pins the actual canonical filename.
        assert_eq!(
            p.file_name().and_then(|s| s.to_str()),
            Some("zshrs.toml"),
            "{:?}",
            p
        );
        // Parent dir is `.zshrs` (the hidden config home), not `zshrs`.
        assert_eq!(
            p.parent()
                .and_then(|d| d.file_name())
                .and_then(|s| s.to_str()),
            Some(".zshrs"),
            "{:?}",
            p
        );
    }

    #[test]
    fn log_level_default_is_info_string() {
        let _g = crate::test_util::global_state_lock();
        let cfg = ZshrsConfig::default();
        assert_eq!(cfg.log.level, "info");
    }
}