sail-solve 0.2.0

Fast, cached Verus verification for Rust: standalone files, projects, watch and agent workflows
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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//! `sail status` and `sail cache`: daemon state and cache maintenance
//! (ADR 0015).
//!
//! Exit codes: 0 success; 2 usage error; 4 no daemon is listening
//! (`sail status`, `SAIL0401`); 5 `sail cache verify` found bad files and
//! did not remove them (`SAIL0501`); 1 any other failure.

use std::fmt::Write as _;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::time::Duration;

use sail_api::protocol::{Info, StatsSnapshot};
use sail_cache::DiskCache;
use sail_cache::maint::Usage;

/// Exit code: no daemon is listening.
pub const EXIT_NO_DAEMON: u8 = 4;
/// Exit code: `sail cache verify` found bad files and left them.
pub const EXIT_BAD_ENTRIES: u8 = 5;
const EXIT_USAGE: u8 = 2;

/// How long `sail status` and `sail cache` wait on the daemon.
const TIMEOUT: Duration = Duration::from_secs(5);

/// Bytes for people: `512 B`, `1.5 KiB`, `20.0 MiB`.
pub fn human(bytes: u64) -> String {
    const UNITS: [&str; 4] = ["KiB", "MiB", "GiB", "TiB"];
    if bytes < 1024 {
        return format!("{bytes} B");
    }
    #[allow(clippy::cast_precision_loss, reason = "a rounded figure for display")]
    let mut v = bytes as f64 / 1024.0;
    let mut unit = UNITS[0];
    for u in UNITS.iter().skip(1) {
        if v < 1024.0 {
            break;
        }
        v /= 1024.0;
        unit = u;
    }
    format!("{v:.1} {unit}")
}

/// Parses a byte count: digits with an optional `K`, `M`, `G` or `T`
/// suffix (powers of 1024), optionally followed by `iB` or `B`.
pub fn parse_bytes(text: &str) -> Option<u64> {
    let t = text.trim();
    let digits = t.bytes().take_while(u8::is_ascii_digit).count();
    let (num, suffix) = t.split_at(digits);
    let n: u64 = num.parse().ok()?;
    let suffix = suffix.trim().to_ascii_lowercase();
    let shift = match suffix.trim_end_matches("ib").trim_end_matches('b') {
        "" => 0,
        "k" => 10,
        "m" => 20,
        "g" => 30,
        "t" => 40,
        _ => return None,
    };
    n.checked_mul(1u64 << shift)
}

fn duration(secs: u64) -> String {
    let (h, m, s) = (secs / 3600, secs / 60 % 60, secs % 60);
    match (h, m) {
        (0, 0) => format!("{s}s"),
        (0, _) => format!("{m}m {s}s"),
        _ => format!("{h}h {m}m {s}s"),
    }
}

/// The daemon socket: `$SAIL_DAEMON_SOCKET`, else the default socket under
/// sail's cache directory.
fn default_socket() -> Result<PathBuf, String> {
    match std::env::var_os("SAIL_DAEMON_SOCKET").filter(|v| !v.is_empty()) {
        Some(s) => Ok(PathBuf::from(s)),
        None => sail_verus::cache_dir()
            .map(|c| crate::default_socket(&c))
            .map_err(|e| e.to_string()),
    }
}

/// What the daemon at `socket` reports: counters, and (from daemons that
/// know `INFO`) its configuration.
#[cfg(unix)]
fn ask(socket: &Path) -> Option<(StatsSnapshot, Option<Info>)> {
    use sail_api::client::Client;
    let mut c = Client::connect_with_timeout(socket, TIMEOUT).ok()?;
    let stats = c.stats().ok()?;
    // A daemon that predates INFO closes the connection.
    let info = c.info().ok();
    Some((stats, info))
}

#[cfg(not(unix))]
fn ask(_socket: &Path) -> Option<(StatsSnapshot, Option<Info>)> {
    None
}

fn usage_error(err: &mut dyn std::io::Write, msg: &str) -> ExitCode {
    let _ = writeln!(err, "error[SAIL0002]: {msg}");
    ExitCode::from(EXIT_USAGE)
}

fn print_json(out: &mut dyn std::io::Write, value: &serde_json::Value) {
    let _ = writeln!(
        out,
        "{}",
        serde_json::to_string_pretty(value).unwrap_or_default()
    );
}

fn status_text(
    socket: &Path,
    info: Option<&Info>,
    disk: Option<Usage>,
    stats: &StatsSnapshot,
) -> String {
    let mut t = String::new();
    let _ = writeln!(t, "sail daemon: running on {}", socket.display());
    match info {
        Some(i) => {
            let _ = writeln!(t, "  pid          {}", i.pid);
            let _ = writeln!(
                t,
                "  version      sail {}, protocol {}",
                i.sail_version, i.protocol_version
            );
            let _ = writeln!(t, "  uptime       {}", duration(i.uptime_secs));
            let _ = writeln!(
                t,
                "  idle stop    {}",
                if i.idle_timeout_secs == 0 {
                    "never".to_owned()
                } else {
                    format!(
                        "after {} without connections",
                        duration(i.idle_timeout_secs)
                    )
                }
            );
            let _ = writeln!(t, "  cache root   {}", i.cache_root);
            if let Some(log) = &i.log {
                let _ = writeln!(t, "  log          {log}");
            }
            if let Some(d) = disk {
                let _ = writeln!(
                    t,
                    "  disk tier    {} entries, {}",
                    d.entries,
                    human(d.bytes)
                );
            }
            let m = i.memory;
            if m.capacity_bytes == 0 {
                let _ = writeln!(t, "  memory tier  off");
            } else {
                let _ = writeln!(
                    t,
                    "  memory tier  {} entries, {} of {} ({} pending write)",
                    m.entries,
                    human(m.bytes),
                    human(m.capacity_bytes),
                    m.pending
                );
            }
            let _ = writeln!(
                t,
                "  write queue  {} entries, {} of {}",
                i.queue.entries,
                human(i.queue.bytes),
                human(i.queue.capacity_bytes)
            );
            shared_status(&mut t, i.shared.as_ref());
            if let Some(bundled) = &i.bundled {
                let _ = writeln!(t, "  bundled      {bundled}");
            }
        }
        None => {
            let _ = writeln!(
                t,
                "  (this daemon predates the INFO operation: no configuration to show)"
            );
        }
    }
    let _ = writeln!(
        t,
        "  since start  {} sessions, {} gets, {} hits ({} from memory), {} puts, {} dropped puts",
        stats.hellos, stats.gets, stats.hits, stats.memory_hits, stats.puts, stats.dropped_puts
    );
    if info.is_some_and(|i| i.shared.is_some()) || stats.shared_lookups > 0 {
        let _ = writeln!(
            t,
            "  shared       {} lookups, {} hits, {} rejected ({} other solver, {} other platform), {} timeouts, {} skipped",
            stats.shared_lookups,
            stats.shared_hits,
            stats.shared_rejected,
            stats.shared_other_solver,
            stats.shared_other_platform,
            stats.shared_timeouts,
            stats.shared_skipped
        );
    }
    if stats.bundled_lookups > 0 {
        let _ = writeln!(
            t,
            "  bundled      {} lookups, {} hits, {} rejected",
            stats.bundled_lookups, stats.bundled_hits, stats.bundled_rejected
        );
    }
    t
}

/// `sail status [--socket PATH] [--json]`.
pub fn status(
    args: &[String],
    out: &mut dyn std::io::Write,
    err: &mut dyn std::io::Write,
) -> ExitCode {
    let (mut socket, mut json) = (None, false);
    let mut it = args.iter();
    while let Some(arg) = it.next() {
        match arg.as_str() {
            "--json" => json = true,
            "--socket" => match it.next() {
                Some(v) => socket = Some(PathBuf::from(v)),
                None => return usage_error(err, "--socket needs a value"),
            },
            other => return usage_error(err, &format!("unknown argument `{other}`")),
        }
    }
    let socket = match socket.map_or_else(default_socket, Ok) {
        Ok(s) => s,
        Err(e) => return usage_error(err, &e),
    };
    let Some((stats, info)) = ask(&socket) else {
        if json {
            print_json(
                out,
                &serde_json::json!({ "running": false, "socket": socket.display().to_string() }),
            );
        }
        let _ = writeln!(
            err,
            "error[SAIL0401]: no sail daemon is listening on {} (`sail serve` starts one; `sail verify` starts one on demand)",
            socket.display()
        );
        return ExitCode::from(EXIT_NO_DAEMON);
    };
    let disk = info.as_ref().map(|i| DiskCache::new(&i.cache_root).usage());
    let preparation = sail_api::jobs::Client::new(sail_api::jobs::socket_path(&socket))
        .call(&sail_api::jobs::Request::PreparationStatus)
        .ok()
        .and_then(|response| match response {
            sail_api::jobs::Response::Preparation(prepared) => Some(prepared),
            _ => None,
        });
    if json {
        print_json(
            out,
            &serde_json::json!({
                "running": true,
                "socket": socket.display().to_string(),
                "info": info,
                "stats": stats,
                "disk": disk,
                "preparation": preparation,
            }),
        );
        return ExitCode::SUCCESS;
    }
    let _ = write!(out, "{}", status_text(&socket, info.as_ref(), disk, &stats));
    ExitCode::SUCCESS
}

/// Parsed `sail cache` options.
#[allow(
    clippy::struct_excessive_bools,
    reason = "independent command-line flags"
)]
#[derive(Default)]
pub(crate) struct CacheArgs {
    sub: Option<String>,
    cache: Option<PathBuf>,
    socket: Option<PathBuf>,
    pub json: bool,
    yes: bool,
    remove: bool,
    max_bytes: Option<u64>,
    /// Positional arguments after the command (`trust add FILE`).
    pub rest: Vec<String>,
    /// `publish --to`: the shared store.
    pub to: Option<PathBuf>,
    /// `publish --key`: the signing key file.
    pub key: Option<PathBuf>,
    /// `keygen --out`: where to write the new key.
    pub out: Option<PathBuf>,
    /// `publish --filter`: `command=NAME` or `solver=SHA256-PREFIX`.
    pub filters: Vec<String>,
    /// `publish --dry-run`.
    pub dry_run: bool,
}

fn parse_cache_args(args: &[String]) -> Result<CacheArgs, String> {
    let mut a = CacheArgs::default();
    let mut it = args.iter();
    while let Some(arg) = it.next() {
        let mut value = |name: &str| {
            it.next()
                .cloned()
                .ok_or_else(|| format!("{name} needs a value"))
        };
        match arg.as_str() {
            "--json" => a.json = true,
            "--yes" => a.yes = true,
            "--remove" => a.remove = true,
            "--cache" => a.cache = Some(PathBuf::from(value("--cache")?)),
            "--socket" => a.socket = Some(PathBuf::from(value("--socket")?)),
            "--max-bytes" => {
                let v = value("--max-bytes")?;
                a.max_bytes = Some(parse_bytes(&v).ok_or_else(|| format!("bad byte count `{v}`"))?);
            }
            "--dry-run" => a.dry_run = true,
            "--to" => a.to = Some(PathBuf::from(value("--to")?)),
            "--key" => a.key = Some(PathBuf::from(value("--key")?)),
            "--out" => a.out = Some(PathBuf::from(value("--out")?)),
            "--filter" => a.filters.push(value("--filter")?),
            s if !s.starts_with('-') && a.sub.is_none() => a.sub = Some(s.to_owned()),
            s if !s.starts_with('-') => a.rest.push(s.to_owned()),
            other => return Err(format!("unknown argument `{other}`")),
        }
    }
    Ok(a)
}

pub(crate) const CACHE_USAGE: &str = "USAGE:
    sail cache [--cache DIR | --socket PATH] [--json] <COMMAND>

COMMANDS:
    stats                  entries and bytes by command, unknown answers, bad files
    verify [--remove]      check every entry's integrity; --remove deletes bad files
    gc --max-bytes N       evict least recently used entries down to N bytes (K/M/G suffixes)
    clear --yes            remove every entry
    projects list|prune    managed runs' compiled targets and receipts, per workspace
                           (`sail cache projects --help`)

The cache is the running daemon's (asked over its socket), or DIR.
";

/// The cache root to work on: `--cache`, else the daemon's, else the
/// default root `sail serve` uses.
fn cache_root(a: &CacheArgs, err: &mut dyn std::io::Write) -> Result<PathBuf, String> {
    if let Some(c) = &a.cache {
        return Ok(c.clone());
    }
    let socket = a.socket.clone().map_or_else(default_socket, Ok)?;
    if let Some((_, Some(info))) = ask(&socket) {
        return Ok(PathBuf::from(info.cache_root));
    }
    let root = sail_verus::cache_dir()
        .map_err(|e| e.to_string())?
        .join("results");
    let _ = writeln!(
        err,
        "note: no daemon answered on {}; using the default cache root {}",
        socket.display(),
        root.display()
    );
    Ok(root)
}

/// Managed runs' per-workspace directories, which the reply entries do not
/// include.
#[derive(serde::Serialize)]
struct ManagedTotal {
    root: PathBuf,
    bytes: u64,
    workspaces: usize,
}

fn managed_total() -> Option<ManagedTotal> {
    let cache = sail_verus::cache_dir().ok()?;
    Some(ManagedTotal {
        workspaces: sail_verus::managed::projects::list(&cache).ok()?.len(),
        bytes: sail_verus::managed::projects::total_bytes(&cache),
        root: sail_verus::managed::projects::root(&cache),
    })
}

fn stats(disk: &DiskCache, root_text: &str, json: bool, out: &mut dyn std::io::Write) -> ExitCode {
    let s = disk.summarize();
    let managed = managed_total();
    if json {
        print_json(
            out,
            &serde_json::json!({ "root": root_text, "stats": s, "managed_projects": managed }),
        );
        return ExitCode::SUCCESS;
    }
    let _ = write!(out, "{}", stats_text(root_text, &s));
    if let Some(m) = managed {
        let _ = writeln!(
            out,
            "managed targets {}: {} in {} workspaces (see `sail cache projects list`)",
            m.root.display(),
            human(m.bytes),
            m.workspaces
        );
    }
    ExitCode::SUCCESS
}

fn usage_line(u: Usage) -> String {
    format!("{} entries, {}", u.entries, human(u.bytes))
}

fn stats_text(root_text: &str, s: &sail_cache::maint::Summary) -> String {
    let mut t = String::new();
    let _ = writeln!(t, "cache {root_text}");
    let _ = writeln!(
        t,
        "  {}",
        usage_line(Usage {
            entries: s.entries,
            bytes: s.bytes
        })
    );
    for (cmd, usage) in &s.by_command {
        let _ = writeln!(t, "    {cmd:<20} {}", usage_line(*usage));
    }
    let u = &s.unknown;
    let _ = writeln!(
        t,
        "  unknown answers: {} ({} with effort, {} used at most half their limit)",
        u.entries, u.with_effort, u.within_half_limit
    );
    for (reason, n) in &u.by_reason {
        let _ = writeln!(t, "    {n:>6}  {reason}");
    }
    if let Some(f) = u.used_fraction {
        let _ = writeln!(
            t,
            "    effort used / limit: min {:.3}, median {:.3}, max {:.3}",
            f.min, f.median, f.max
        );
    }
    let _ = writeln!(
        t,
        "  invalid files: {} (see `sail cache verify`); temporary files: {}",
        s.invalid, s.temp_files
    );
    t
}

fn verify(
    disk: &DiskCache,
    root_text: &str,
    a: &CacheArgs,
    out: &mut dyn std::io::Write,
    err: &mut dyn std::io::Write,
) -> ExitCode {
    let r = disk.verify(a.remove);
    let left = u64::try_from(r.bad.len()).unwrap_or(u64::MAX) - r.removed;
    if a.json {
        print_json(out, &serde_json::json!({ "root": root_text, "verify": r }));
    } else {
        let mut t = String::new();
        let _ = writeln!(
            t,
            "cache {root_text}: checked {} entries, {} valid, {} bad files ({} removed)",
            r.checked,
            r.valid,
            r.bad.len(),
            r.removed
        );
        for b in &r.bad {
            let _ = writeln!(
                t,
                "  {:<14} {}{}",
                b.problem.name(),
                b.path,
                if b.removed { " (removed)" } else { "" }
            );
        }
        let _ = write!(out, "{t}");
    }
    if left > 0 {
        let _ = writeln!(
            err,
            "error[SAIL0501]: {left} bad cache files; `sail cache verify --remove` deletes them (each is a miss, never a wrong answer)"
        );
        return ExitCode::from(EXIT_BAD_ENTRIES);
    }
    ExitCode::SUCCESS
}

/// `sail cache ...`.
pub fn cache(
    args: &[String],
    out: &mut dyn std::io::Write,
    err: &mut dyn std::io::Write,
) -> ExitCode {
    if args.first().is_some_and(|a| a == "projects") {
        return crate::projects_cli::run(&args[1..], out, err);
    }
    if let Some(code) = crate::remote_cache::cache(args, out, err) {
        return code;
    }
    let a = match parse_cache_args(args) {
        Ok(a) => a,
        Err(e) => {
            let _ = writeln!(err, "error[SAIL0002]: {e}\n\n{CACHE_USAGE}");
            return ExitCode::from(EXIT_USAGE);
        }
    };
    let Some(sub) = a.sub.clone() else {
        let _ = write!(err, "{CACHE_USAGE}");
        return ExitCode::from(EXIT_USAGE);
    };
    if sub == "keygen" {
        return crate::shared_ops::keygen(&a, out, err);
    }
    if !matches!(
        sub.as_str(),
        "stats" | "verify" | "gc" | "clear" | "trust" | "publish" | "bundle-replies"
    ) {
        let _ = writeln!(
            err,
            "error[SAIL0002]: unknown cache command `{sub}`\n\n{CACHE_USAGE}"
        );
        return ExitCode::from(EXIT_USAGE);
    }
    if sub == "gc" && a.max_bytes.is_none() {
        return usage_error(err, "`sail cache gc` needs --max-bytes N");
    }
    if sub == "clear" && !a.yes {
        let _ = writeln!(
            err,
            "error[SAIL0502]: `sail cache clear` removes every entry; pass --yes to confirm"
        );
        return ExitCode::from(EXIT_USAGE);
    }
    let root = match cache_root(&a, err) {
        Ok(r) => r,
        Err(e) => return usage_error(err, &e),
    };
    let disk = DiskCache::new(&root);
    let root_text = root.display().to_string();
    match sub.as_str() {
        "trust" => crate::shared_ops::trust(&root, &a, out, err),
        "publish" => crate::shared_ops::publish(&disk, &a, out, err),
        "bundle-replies" => crate::shared_ops::bundle_replies(&disk, &a, out, err),
        "stats" => stats(&disk, &root_text, a.json, out),
        "verify" => verify(&disk, &root_text, &a, out, err),
        "gc" => {
            let r = disk.gc(a.max_bytes.unwrap_or(u64::MAX));
            if a.json {
                print_json(out, &serde_json::json!({ "root": root_text, "gc": r }));
            } else {
                let _ = writeln!(
                    out,
                    "cache {root_text}: evicted {}; {} remain (budget {}); {} stale temporary files removed",
                    usage_line(r.evicted),
                    usage_line(r.after),
                    human(r.max_bytes),
                    r.temp_removed
                );
            }
            ExitCode::SUCCESS
        }
        _ => match disk.clear() {
            Ok(u) => {
                if a.json {
                    print_json(out, &serde_json::json!({ "root": root_text, "cleared": u }));
                } else {
                    let _ = writeln!(out, "cache {root_text}: removed {}", usage_line(u));
                }
                ExitCode::SUCCESS
            }
            Err(e) => {
                let _ = writeln!(err, "error[SAIL0503]: cannot clear {root_text}: {e}");
                ExitCode::from(1)
            }
        },
    }
}

fn shared_status(t: &mut String, shared: Option<&sail_api::protocol::SharedInfo>) {
    match shared {
        Some(s) => {
            let _ = writeln!(
                t,
                "  shared tier  {}, {} trusted keys ({}), timeout {} ms",
                s.store,
                s.trusted_keys.len(),
                s.trust_dir,
                s.timeout_ms
            );
            if let Some(remote) = &s.remote {
                let _ = writeln!(
                    t,
                    "  remote       {} requests, {} auth refused, {} unavailable, {} coalesced; last {}",
                    remote.requests,
                    remote.auth_refused,
                    remote.unavailable,
                    remote.coalesced,
                    remote.last_status.as_deref().unwrap_or("none")
                );
            }
        }
        None => {
            let _ = writeln!(t, "  shared tier  off");
        }
    }
}

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

    #[test]
    fn byte_counts_parse_with_suffixes() {
        assert_eq!(parse_bytes("123"), Some(123));
        assert_eq!(parse_bytes("64K"), Some(64 * 1024));
        assert_eq!(parse_bytes("64KiB"), Some(64 * 1024));
        assert_eq!(parse_bytes("2mb"), Some(2 << 20));
        assert_eq!(parse_bytes("1G"), Some(1 << 30));
        assert_eq!(parse_bytes("1X"), None);
        assert_eq!(parse_bytes(""), None);
        assert_eq!(parse_bytes("99999999999999T"), None);
    }

    #[test]
    fn human_sizes() {
        assert_eq!(human(10), "10 B");
        assert_eq!(human(1536), "1.5 KiB");
        assert_eq!(human(3 << 20), "3.0 MiB");
        assert_eq!(duration(3725), "1h 2m 5s");
        assert_eq!(duration(65), "1m 5s");
    }
}