vetto 0.2.13

Daemon-less sandbox + security layer for AI coding agents (Landlock/Seatbelt, TUI statusline, post-session audit reports)
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
//! Multi-shell environment hook generator (Step 17).
//!
//! Generates and manages environment integration snippets for Bash, Zsh,
//! Fish, PowerShell, and CMD across Unix and Windows platforms.

use anyhow::{Context, Result};
use std::fs;
use std::path::{Path, PathBuf};

pub const MARKER_START: &str = "# >>> vetto shim environment >>>";
pub const MARKER_END: &str = "# <<< vetto shim environment <<<";

pub const CMD_MARKER_START: &str = "rem >>> vetto shim environment >>>";
pub const CMD_MARKER_END: &str = "rem <<< vetto shim environment <<<";

/// Supported shell kinds.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Hash, clap::ValueEnum, serde::Serialize, serde::Deserialize,
)]
#[serde(rename_all = "lowercase")]
pub enum ShellKind {
    Bash,
    Zsh,
    Fish,
    #[value(name = "powershell", alias = "pwsh")]
    PowerShell,
    Cmd,
}

impl ShellKind {
    pub fn all() -> &'static [ShellKind] {
        &[
            ShellKind::Bash,
            ShellKind::Zsh,
            ShellKind::Fish,
            ShellKind::PowerShell,
            ShellKind::Cmd,
        ]
    }

    pub fn name(&self) -> &'static str {
        match self {
            ShellKind::Bash => "bash",
            ShellKind::Zsh => "zsh",
            ShellKind::Fish => "fish",
            ShellKind::PowerShell => "powershell",
            ShellKind::Cmd => "cmd",
        }
    }
}

/// Status of shell environment integration.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ShellHookStatus {
    pub shell: ShellKind,
    pub profile_path: PathBuf,
    pub profile_exists: bool,
    pub is_installed: bool,
}

/// Generates shell integration snippet for the specified shell kind.
pub fn generate_snippet(shell: ShellKind, shims_dir: &Path) -> String {
    let shims_str = shims_dir.display().to_string();

    match shell {
        ShellKind::Bash | ShellKind::Zsh => {
            format!(
                "{MARKER_START}\n\
# Automatically generated by `vetto hook install`. Do not edit manually.\n\
if [ -d \"{shims_str}\" ]; then\n\
    case \":$PATH:\" in\n\
        *\":{shims_str}:\"*) ;;\n\
        *) export PATH=\"{shims_str}:$PATH\" ;;\n\
    esac\n\
fi\n\
{MARKER_END}\n"
            )
        }
        ShellKind::Fish => {
            format!(
                "{MARKER_START}\n\
# Automatically generated by `vetto hook install`. Do not edit manually.\n\
if test -d \"{shims_str}\"\n\
    if not contains \"{shims_str}\" $PATH\n\
        set -gx PATH \"{shims_str}\" $PATH\n\
    end\n\
end\n\
{MARKER_END}\n"
            )
        }
        ShellKind::PowerShell => {
            format!(
                "{MARKER_START}\n\
# Automatically generated by `vetto hook install`. Do not edit manually.\n\
$vettoShims = \"{shims_str}\"\n\
if (Test-Path $vettoShims) {{\n\
    if (-not ($env:PATH -split ';' -contains $vettoShims)) {{\n\
        $env:PATH = \"$vettoShims;$env:PATH\"\n\
    }}\n\
}}\n\
{MARKER_END}\n"
            )
        }
        ShellKind::Cmd => {
            format!(
                "{CMD_MARKER_START}\n\
rem Automatically generated by `vetto hook install`. Do not edit manually.\n\
if exist \"{shims_str}\" (\n\
    echo %PATH% | findstr /i /c:\"{shims_str}\" >nul || set PATH={shims_str};%PATH%\n\
)\n\
{CMD_MARKER_END}\n"
            )
        }
    }
}

/// Returns potential configuration file paths for a shell within a home directory.
pub fn profile_paths_for_shell(shell: ShellKind, home_dir: &Path) -> Vec<PathBuf> {
    match shell {
        ShellKind::Bash => {
            vec![
                home_dir.join(".bashrc"),
                home_dir.join(".bash_profile"),
                home_dir.join(".profile"),
            ]
        }
        ShellKind::Zsh => {
            vec![home_dir.join(".zshrc")]
        }
        ShellKind::Fish => {
            vec![home_dir.join(".config").join("fish").join("config.fish")]
        }
        ShellKind::PowerShell => {
            #[cfg(windows)]
            {
                vec![
                    home_dir
                        .join("Documents")
                        .join("PowerShell")
                        .join("Microsoft.PowerShell_profile.ps1"),
                    home_dir
                        .join("Documents")
                        .join("WindowsPowerShell")
                        .join("Microsoft.PowerShell_profile.ps1"),
                ]
            }
            #[cfg(not(windows))]
            {
                vec![home_dir
                    .join(".config")
                    .join("powershell")
                    .join("Microsoft.PowerShell_profile.ps1")]
            }
        }
        ShellKind::Cmd => {
            vec![home_dir.join(".vetto").join("vetto_env.cmd")]
        }
    }
}

/// Resolves the primary profile path for the shell (creating parent directories if needed).
pub fn primary_profile_path(shell: ShellKind, home_dir: &Path) -> PathBuf {
    let candidates = profile_paths_for_shell(shell, home_dir);
    for candidate in &candidates {
        if candidate.exists() {
            return candidate.clone();
        }
    }
    candidates[0].clone()
}

/// Installs the Vetto shell environment hook into the shell's profile file.
pub fn install_shell_hook(
    shell: ShellKind,
    shims_dir: &Path,
    home_dir: &Path,
    force: bool,
) -> Result<PathBuf> {
    let profile_path = primary_profile_path(shell, home_dir);

    if let Some(parent) = profile_path.parent() {
        fs::create_dir_all(parent)
            .with_context(|| format!("failed to create config directory {}", parent.display()))?;
    }

    let existing_content = if profile_path.exists() {
        fs::read_to_string(&profile_path)
            .with_context(|| format!("failed to read {}", profile_path.display()))?
    } else {
        String::new()
    };

    let (start_marker, end_marker) = if shell == ShellKind::Cmd {
        (CMD_MARKER_START, CMD_MARKER_END)
    } else {
        (MARKER_START, MARKER_END)
    };

    let snippet = generate_snippet(shell, shims_dir);

    let new_content = if let (Some(start_idx), Some(end_idx)) = (
        existing_content.find(start_marker),
        existing_content.find(end_marker),
    ) {
        if !force {
            // Already installed and force not requested
            return Ok(profile_path);
        }
        let after_end = end_idx + end_marker.len();
        let end_of_line = existing_content[after_end..]
            .find('\n')
            .map(|i| after_end + i + 1)
            .unwrap_or(existing_content.len());

        let mut content = existing_content[..start_idx].to_string();
        content.push_str(&snippet);
        content.push_str(&existing_content[end_of_line..]);
        content
    } else {
        let mut content = existing_content;
        if !content.is_empty() && !content.ends_with('\n') {
            content.push('\n');
        }
        content.push_str(&snippet);
        content
    };

    fs::write(&profile_path, new_content)
        .with_context(|| format!("failed to write {}", profile_path.display()))?;

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let _ = fs::set_permissions(&profile_path, fs::Permissions::from_mode(0o644));
    }

    Ok(profile_path)
}

/// Uninstalls the Vetto shell environment hook from the shell's profile.
pub fn uninstall_shell_hook(shell: ShellKind, home_dir: &Path) -> Result<Option<PathBuf>> {
    let candidates = profile_paths_for_shell(shell, home_dir);
    let (start_marker, end_marker) = if shell == ShellKind::Cmd {
        (CMD_MARKER_START, CMD_MARKER_END)
    } else {
        (MARKER_START, MARKER_END)
    };

    for profile_path in candidates {
        if !profile_path.exists() {
            continue;
        }

        let content = fs::read_to_string(&profile_path)
            .with_context(|| format!("failed to read {}", profile_path.display()))?;

        if let (Some(start_idx), Some(end_idx)) =
            (content.find(start_marker), content.find(end_marker))
        {
            let after_end = end_idx + end_marker.len();
            let end_of_line = content[after_end..]
                .find('\n')
                .map(|i| after_end + i + 1)
                .unwrap_or(content.len());

            let mut cleaned = content[..start_idx].to_string();
            cleaned.push_str(&content[end_of_line..]);

            fs::write(&profile_path, cleaned)
                .with_context(|| format!("failed to update {}", profile_path.display()))?;

            return Ok(Some(profile_path));
        }
    }

    Ok(None)
}

/// Checks whether the shell integration hook is installed for a given shell.
pub fn check_shell_hook_status(
    shell: ShellKind,
    home_dir: &Path,
    _shims_dir: &Path,
) -> ShellHookStatus {
    let profile_path = primary_profile_path(shell, home_dir);
    let (start_marker, end_marker) = if shell == ShellKind::Cmd {
        (CMD_MARKER_START, CMD_MARKER_END)
    } else {
        (MARKER_START, MARKER_END)
    };

    let mut is_installed = false;
    let profile_exists = profile_path.exists();

    if profile_exists {
        if let Ok(content) = fs::read_to_string(&profile_path) {
            if content.contains(start_marker) && content.contains(end_marker) {
                is_installed = true;
            }
        }
    }

    ShellHookStatus {
        shell,
        profile_path,
        profile_exists,
        is_installed,
    }
}

/// Detects available shells on the current system.
pub fn detect_available_shells(home_dir: &Path) -> Vec<ShellKind> {
    let mut detected = Vec::new();

    for &shell in ShellKind::all() {
        let candidates = profile_paths_for_shell(shell, home_dir);
        let config_exists = candidates.iter().any(|c| c.exists());

        let binary_exists = {
            #[cfg(unix)]
            {
                let name = shell.name();
                Path::new(&format!("/bin/{name}")).exists()
                    || Path::new(&format!("/usr/bin/{name}")).exists()
                    || Path::new(&format!("/usr/local/bin/{name}")).exists()
            }
            #[cfg(windows)]
            {
                matches!(shell, ShellKind::PowerShell | ShellKind::Cmd)
            }
        };

        if config_exists || binary_exists {
            detected.push(shell);
        }
    }

    if detected.is_empty() {
        #[cfg(unix)]
        detected.push(ShellKind::Bash);
        #[cfg(windows)]
        detected.push(ShellKind::PowerShell);
    }

    detected
}

/// Generate environment variable export lines for shell PS1 / prompt integration.
pub fn emit_shell_env(
    session_id: Option<&str>,
    tier: Option<&str>,
    profile: Option<&str>,
) -> String {
    let sid = session_id.unwrap_or("active");
    let t = tier.unwrap_or("full");
    let p = profile.unwrap_or("default");
    format!(
        "export VETTO_SANDBOX=1\n\
         export VETTO_SESSION_ID=\"{sid}\"\n\
         export VETTO_TIER=\"{t}\"\n\
         export VETTO_PROFILE=\"{p}\"\n\
         export VETTO_VERSION=\"{}\"\n",
        env!("CARGO_PKG_VERSION")
    )
}

/// Print shell environment export lines to stdout.
pub fn run_shell_env(
    session_id: Option<&str>,
    tier: Option<&str>,
    profile: Option<&str>,
) -> Result<()> {
    print!("{}", emit_shell_env(session_id, tier, profile));
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::time::{SystemTime, UNIX_EPOCH};

    fn temp_test_dir(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "vetto-shell-env-{name}-{}",
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[test]
    fn generates_snippets_for_all_shells() {
        let shims = Path::new("/home/user/.vetto/shims");
        for &shell in ShellKind::all() {
            let snippet = generate_snippet(shell, shims);
            assert!(snippet.contains("/home/user/.vetto/shims"));
            if shell == ShellKind::Cmd {
                assert!(snippet.contains(CMD_MARKER_START));
                assert!(snippet.contains(CMD_MARKER_END));
            } else {
                assert!(snippet.contains(MARKER_START));
                assert!(snippet.contains(MARKER_END));
            }
        }
    }

    #[test]
    fn installs_and_uninstalls_bash_hook_cleanly() {
        let home = temp_test_dir("bash-test");
        let shims = home.join(".vetto").join("shims");
        let bashrc = home.join(".bashrc");

        // Initial content
        fs::write(&bashrc, "export FOO=bar\n").unwrap();

        // Install
        let path = install_shell_hook(ShellKind::Bash, &shims, &home, false).unwrap();
        assert_eq!(path, bashrc);

        let content = fs::read_to_string(&bashrc).unwrap();
        assert!(content.starts_with("export FOO=bar\n"));
        assert!(content.contains(MARKER_START));

        let status = check_shell_hook_status(ShellKind::Bash, &home, &shims);
        assert!(status.is_installed);
        assert!(status.profile_exists);

        // Uninstall
        let uninstalled = uninstall_shell_hook(ShellKind::Bash, &home).unwrap();
        assert_eq!(uninstalled, Some(bashrc.clone()));

        let cleaned_content = fs::read_to_string(&bashrc).unwrap();
        assert_eq!(cleaned_content.trim(), "export FOO=bar");

        let status_after = check_shell_hook_status(ShellKind::Bash, &home, &shims);
        assert!(!status_after.is_installed);

        let _ = fs::remove_dir_all(&home);
    }
}