vetto 0.2.19

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
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
//! Inter-process advisory session locking for concurrent rescue operations.
//!
//! Provides cross-platform non-blocking file locking with lease management,
//! stale lock detection (via PID liveness probing and lease expiration),
//! exponential backoff retries, and clean RAII unlock guards.
//!
//! - Linux: Open File Description locks (`F_OFD_SETLK`) with fallback to `flock`.
//! - macOS: `flock(LOCK_EX | LOCK_NB)`.
//! - Windows: `LockFileEx(LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY)`.

use std::fs::{self, File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::thread;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};

#[cfg(unix)]
use std::os::unix::io::AsRawFd;

#[cfg(windows)]
use std::os::windows::io::AsRawHandle;

pub const DEFAULT_LEASE_TIMEOUT_MS: u64 = 30_000;
pub const DEFAULT_LOCK_FILENAME: &str = ".vetto_repair.lock";

/// Lockfile payload storing process lease information.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct LockMetadata {
    pub pid: u32,
    pub acquired_at: u64,
    pub lease_timeout_ms: u64,
}

impl LockMetadata {
    pub fn is_expired(&self) -> bool {
        let now = now_unix_secs();
        let timeout_secs = (self.lease_timeout_ms / 1000).max(1);
        now > self.acquired_at.saturating_add(timeout_secs)
    }
}

/// RAII Guard that releases the underlying OS file lock and cleans up the
/// lockfile on drop.
pub struct SessionLockGuard {
    lock_file: File,
    lock_path: PathBuf,
    metadata: LockMetadata,
}

impl std::fmt::Debug for SessionLockGuard {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SessionLockGuard")
            .field("lock_path", &self.lock_path)
            .field("metadata", &self.metadata)
            .finish()
    }
}

impl SessionLockGuard {
    /// Attempt to acquire an advisory exclusive lock immediately without blocking.
    pub fn try_acquire(lock_path: &Path, lease_timeout_ms: u64) -> Result<Self> {
        if let Some(parent) = lock_path.parent() {
            fs::create_dir_all(parent)
                .with_context(|| format!("create lock directory {}", parent.display()))?;
        }

        let mut file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(lock_path)
            .with_context(|| format!("open lockfile {}", lock_path.display()))?;

        match try_os_lock(&file) {
            Ok(true) => {
                let metadata = LockMetadata {
                    pid: std::process::id(),
                    acquired_at: now_unix_secs(),
                    lease_timeout_ms,
                };
                write_lock_metadata(&mut file, &metadata)?;
                Ok(Self {
                    lock_file: file,
                    lock_path: lock_path.to_path_buf(),
                    metadata,
                })
            }
            Ok(false) => {
                // OS lock failed. Inspect existing lease metadata to check if stale.
                let mut content = String::new();
                let _ = file.read_to_string(&mut content);
                if let Ok(meta) = serde_json::from_str::<LockMetadata>(&content) {
                    if !is_process_alive(meta.pid) || meta.is_expired() {
                        // Stale lock detected; wait briefly and try once more.
                        thread::sleep(Duration::from_millis(50));
                        if try_os_lock(&file)? {
                            let metadata = LockMetadata {
                                pid: std::process::id(),
                                acquired_at: now_unix_secs(),
                                lease_timeout_ms,
                            };
                            write_lock_metadata(&mut file, &metadata)?;
                            return Ok(Self {
                                lock_file: file,
                                lock_path: lock_path.to_path_buf(),
                                metadata,
                            });
                        }
                    }
                }
                bail!("lock on {} is held by another process", lock_path.display())
            }
            Err(err) => Err(err),
        }
    }

    /// Acquire the advisory lock, retrying with exponential backoff up to `max_wait`.
    pub fn acquire_with_timeout(
        lock_path: &Path,
        lease_timeout_ms: u64,
        max_wait: Duration,
    ) -> Result<Self> {
        let start = std::time::Instant::now();
        let mut backoff = Duration::from_millis(10);
        let max_backoff = Duration::from_millis(500);

        loop {
            match Self::try_acquire(lock_path, lease_timeout_ms) {
                Ok(guard) => return Ok(guard),
                Err(_) if start.elapsed() < max_wait => {
                    thread::sleep(backoff);
                    backoff = (backoff * 2).min(max_backoff);
                }
                Err(err) => {
                    bail!(
                        "timed out after {:?} attempting to acquire lock on {}: {}",
                        start.elapsed(),
                        lock_path.display(),
                        err
                    );
                }
            }
        }
    }

    pub fn metadata(&self) -> &LockMetadata {
        &self.metadata
    }

    pub fn lock_path(&self) -> &Path {
        &self.lock_path
    }
}

impl Drop for SessionLockGuard {
    fn drop(&mut self) {
        let _ = unlock_os_lock(&self.lock_file);
        let _ = fs::remove_file(&self.lock_path);
    }
}

fn now_unix_secs() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

fn write_lock_metadata(file: &mut File, metadata: &LockMetadata) -> Result<()> {
    file.set_len(0).context("truncate lockfile")?;
    file.seek(SeekFrom::Start(0)).context("seek lockfile")?;
    let payload = serde_json::to_vec_pretty(metadata).context("serialize lock metadata")?;
    file.write_all(&payload).context("write lock metadata")?;
    file.sync_all().context("sync lock metadata")?;
    Ok(())
}

// ---------------------------------------------------------------------------
// Platform OS file locking
// ---------------------------------------------------------------------------

#[cfg(target_os = "linux")]
fn try_os_lock(file: &File) -> Result<bool> {
    const F_OFD_SETLK: libc::c_int = 37;
    let fd = file.as_raw_fd();
    let mut fl: libc::flock = unsafe { std::mem::zeroed() };
    fl.l_type = libc::F_WRLCK as i16;
    fl.l_whence = libc::SEEK_SET as i16;
    fl.l_start = 0;
    fl.l_len = 0;
    fl.l_pid = 0;

    let res = unsafe { libc::fcntl(fd, F_OFD_SETLK, &fl) };
    if res == 0 {
        return Ok(true);
    }
    let errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
    if errno == libc::EACCES || errno == libc::EAGAIN || errno == libc::EBUSY {
        return Ok(false);
    }
    // If OFD locks return EINVAL (older kernel or unsupported filesystem), fallback to flock.
    if errno == libc::EINVAL || errno == libc::ENOSYS {
        let flock_res = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
        if flock_res == 0 {
            return Ok(true);
        }
        let flock_err = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
        if flock_err == libc::EWOULDBLOCK || flock_err == libc::EAGAIN {
            return Ok(false);
        }
    }
    bail!(
        "fcntl(F_OFD_SETLK) failed: {}",
        std::io::Error::last_os_error()
    )
}

#[cfg(target_os = "linux")]
fn unlock_os_lock(file: &File) -> Result<()> {
    const F_OFD_SETLK: libc::c_int = 37;
    let fd = file.as_raw_fd();
    let mut fl: libc::flock = unsafe { std::mem::zeroed() };
    fl.l_type = libc::F_UNLCK as i16;
    fl.l_whence = libc::SEEK_SET as i16;
    fl.l_start = 0;
    fl.l_len = 0;
    fl.l_pid = 0;
    unsafe {
        let _ = libc::fcntl(fd, F_OFD_SETLK, &fl);
        let _ = libc::flock(fd, libc::LOCK_UN);
    }
    Ok(())
}

#[cfg(all(unix, not(target_os = "linux")))]
fn try_os_lock(file: &File) -> Result<bool> {
    let fd = file.as_raw_fd();
    let res = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
    if res == 0 {
        return Ok(true);
    }
    let errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
    if errno == libc::EWOULDBLOCK || errno == libc::EAGAIN {
        return Ok(false);
    }
    bail!("flock failed: {}", std::io::Error::last_os_error())
}

#[cfg(all(unix, not(target_os = "linux")))]
fn unlock_os_lock(file: &File) -> Result<()> {
    let fd = file.as_raw_fd();
    unsafe {
        let _ = libc::flock(fd, libc::LOCK_UN);
    }
    Ok(())
}

#[cfg(windows)]
mod win_lock {
    use std::os::windows::io::RawHandle;

    pub const LOCKFILE_EXCLUSIVE_LOCK: u32 = 0x00000002;
    pub const LOCKFILE_FAIL_IMMEDIATELY: u32 = 0x00000001;
    pub const PROCESS_QUERY_LIMITED_INFORMATION: u32 = 0x1000;
    pub const STILL_ACTIVE: u32 = 259;
    pub const ERROR_LOCK_VIOLATION: u32 = 33;

    #[repr(C)]
    // Mirrors the Win32 `OVERLAPPED` declaration verbatim; the acronym is
    // the platform's name, not our style choice.
    #[allow(clippy::upper_case_acronyms)]
    pub struct OVERLAPPED {
        pub internal: usize,
        pub internal_high: usize,
        pub offset: u32,
        pub offset_high: u32,
        pub h_event: usize,
    }

    extern "system" {
        pub fn LockFileEx(
            hFile: RawHandle,
            dwFlags: u32,
            dwReserved: u32,
            nNumberOfBytesToLockLow: u32,
            nNumberOfBytesToLockHigh: u32,
            lpOverlapped: *mut OVERLAPPED,
        ) -> i32;

        pub fn UnlockFileEx(
            hFile: RawHandle,
            dwReserved: u32,
            nNumberOfBytesToUnlockLow: u32,
            nNumberOfBytesToUnlockHigh: u32,
            lpOverlapped: *mut OVERLAPPED,
        ) -> i32;

        pub fn OpenProcess(
            dwDesiredAccess: u32,
            bInheritHandle: i32,
            dwProcessId: u32,
        ) -> RawHandle;

        pub fn GetExitCodeProcess(hProcess: RawHandle, lpExitCode: *mut u32) -> i32;

        pub fn CloseHandle(hObject: RawHandle) -> i32;
    }
}

#[cfg(windows)]
fn try_os_lock(file: &File) -> Result<bool> {
    let handle = file.as_raw_handle();
    let mut overlapped: win_lock::OVERLAPPED = unsafe { std::mem::zeroed() };
    let flags = win_lock::LOCKFILE_EXCLUSIVE_LOCK | win_lock::LOCKFILE_FAIL_IMMEDIATELY;
    let res = unsafe {
        win_lock::LockFileEx(
            handle,
            flags,
            0,
            1,
            0,
            &mut overlapped as *mut win_lock::OVERLAPPED,
        )
    };
    if res != 0 {
        return Ok(true);
    }
    let err = std::io::Error::last_os_error();
    if err.raw_os_error() == Some(win_lock::ERROR_LOCK_VIOLATION as i32) {
        return Ok(false);
    }
    bail!("LockFileEx failed: {}", err)
}

#[cfg(windows)]
fn unlock_os_lock(file: &File) -> Result<()> {
    let handle = file.as_raw_handle();
    let mut overlapped: win_lock::OVERLAPPED = unsafe { std::mem::zeroed() };
    unsafe {
        win_lock::UnlockFileEx(
            handle,
            0,
            1,
            0,
            &mut overlapped as *mut win_lock::OVERLAPPED,
        );
    }
    Ok(())
}

#[cfg(not(any(unix, windows)))]
fn try_os_lock(_file: &File) -> Result<bool> {
    Ok(true)
}

#[cfg(not(any(unix, windows)))]
fn unlock_os_lock(_file: &File) -> Result<()> {
    Ok(())
}

// ---------------------------------------------------------------------------
// PID Liveness probe
// ---------------------------------------------------------------------------

#[cfg(unix)]
pub fn is_process_alive(pid: u32) -> bool {
    if pid == 0 {
        return false;
    }
    let res = unsafe { libc::kill(pid as libc::pid_t, 0) };
    if res == 0 {
        true
    } else {
        let err = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
        err == libc::EPERM
    }
}

#[cfg(windows)]
pub fn is_process_alive(pid: u32) -> bool {
    if pid == 0 {
        return false;
    }
    unsafe {
        let handle = win_lock::OpenProcess(win_lock::PROCESS_QUERY_LIMITED_INFORMATION, 0, pid);
        if handle.is_null() {
            return false;
        }
        let mut exit_code: u32 = 0;
        let ok = win_lock::GetExitCodeProcess(handle, &mut exit_code);
        win_lock::CloseHandle(handle);
        ok != 0 && exit_code == win_lock::STILL_ACTIVE
    }
}

#[cfg(not(any(unix, windows)))]
pub fn is_process_alive(_pid: u32) -> bool {
    false
}

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

    fn temp_lock_path(tag: &str) -> PathBuf {
        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        std::env::temp_dir().join(format!(
            "vetto-lock-{}-{}-{}.lock",
            tag,
            std::process::id(),
            nonce
        ))
    }

    #[test]
    fn acquires_and_releases_session_lock() {
        let path = temp_lock_path("basic");
        {
            let guard = SessionLockGuard::try_acquire(&path, 30_000).expect("acquire lock");
            assert_eq!(guard.metadata().pid, std::process::id());
            assert!(path.exists());

            // Contention: second acquire on same path fails
            let err = SessionLockGuard::try_acquire(&path, 30_000).expect_err("contention");
            assert!(err.to_string().contains("held by another process"));
        }
        // After drop, lockfile can be acquired again
        let guard2 = SessionLockGuard::try_acquire(&path, 30_000).expect("re-acquire lock");
        assert_eq!(guard2.metadata().pid, std::process::id());
    }

    #[test]
    fn detects_stale_pid_and_reclaims_lock() {
        let path = temp_lock_path("stale");
        // Write a fake stale lock from an impossible PID (e.g. 9999999)
        let stale_meta = LockMetadata {
            pid: 9_999_999,
            acquired_at: now_unix_secs().saturating_sub(100),
            lease_timeout_ms: 10_000,
        };
        fs::write(&path, serde_json::to_string_pretty(&stale_meta).unwrap()).unwrap();

        // Should successfully acquire because PID is dead/lease expired
        let guard = SessionLockGuard::try_acquire(&path, 30_000).expect("reclaim stale");
        assert_eq!(guard.metadata().pid, std::process::id());
    }

    #[test]
    fn acquire_with_timeout_succeeds_after_release() {
        let path = temp_lock_path("timeout");
        let guard = SessionLockGuard::try_acquire(&path, 30_000).expect("first lock");

        let handle = std::thread::spawn(move || {
            std::thread::sleep(Duration::from_millis(50));
            drop(guard);
        });

        let guard2 = SessionLockGuard::acquire_with_timeout(&path, 30_000, Duration::from_secs(2))
            .expect("acquire with timeout");
        assert_eq!(guard2.metadata().pid, std::process::id());
        handle.join().unwrap();
    }
}