corgi-build 0.1.49

PoC: deterministic, content-addressed, lock-free cargo-compatible build tool
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
use anyhow::{anyhow, Context, Result};
use sha2::{Digest, Sha256};
use std::fs;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

pub fn default_root() -> Result<PathBuf> {
    if let Some(root) = std::env::var_os("CORGI_STORE") {
        return Ok(PathBuf::from(root));
    }
    if cfg!(target_os = "macos") {
        Ok(PathBuf::from("/Users/Shared/corgi"))
    } else {
        let home = std::env::var_os("HOME").context("HOME not set")?;
        Ok(PathBuf::from(home).join(".cache/corgi"))
    }
}

/// Hash-work counters included in performance reports (relaxed: stats only).
pub static STAT_FILES: AtomicU64 = AtomicU64::new(0);
pub static REHASHED_FILES: AtomicU64 = AtomicU64::new(0);
pub static HINTED_DIRS: AtomicU64 = AtomicU64::new(0);
pub static IMMUTABLE_HITS: AtomicU64 = AtomicU64::new(0);
pub static EXPORT_CHECK_BYTES: AtomicU64 = AtomicU64::new(0);

pub fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}

pub fn sha256_hex(data: &[u8]) -> String {
    hex(&Sha256::digest(data))
}

pub fn sha256_file(path: &Path) -> Result<String> {
    let mut f = fs::File::open(path).with_context(|| format!("open {}", path.display()))?;
    let mut h = Sha256::new();
    let mut buf = [0u8; 1 << 16];
    loop {
        let n = f.read(&mut buf)?;
        if n == 0 {
            break;
        }
        h.update(&buf[..n]);
    }
    Ok(hex(&h.finalize()))
}

/// Stream-hash a file while checking for a forbidden byte sequence
/// (needle straddling chunk boundaries included). Returns (hash, found).
pub fn sha256_file_scan(path: &Path, needle: &[u8]) -> Result<(String, bool)> {
    let mut f = fs::File::open(path).with_context(|| format!("open {}", path.display()))?;
    let mut h = Sha256::new();
    let finder = memchr::memmem::Finder::new(needle);
    let overlap = needle.len().saturating_sub(1);
    let mut carry: Vec<u8> = Vec::with_capacity(overlap * 2);
    let mut found = needle.is_empty();
    let mut buf = [0u8; 1 << 16];
    loop {
        let n = f.read(&mut buf)?;
        if n == 0 {
            break;
        }
        h.update(&buf[..n]);
        if !found {
            // A match can straddle the boundary: search the carried tail
            // plus the head of this chunk, then the chunk itself.
            carry.extend_from_slice(&buf[..n.min(overlap)]);
            found = finder.find(&carry).is_some() || finder.find(&buf[..n]).is_some();
            carry.clear();
            carry.extend_from_slice(&buf[n.saturating_sub(overlap)..n]);
        }
    }
    Ok((hex(&h.finalize()), found))
}

pub(crate) struct StreamedInsert {
    path: Option<PathBuf>,
    file: fs::File,
    digest: Sha256,
    bytes: u64,
}

impl Write for StreamedInsert {
    fn write(&mut self, buffer: &[u8]) -> std::io::Result<usize> {
        let written = self.file.write(buffer)?;
        self.digest.update(&buffer[..written]);
        self.bytes += written as u64;
        Ok(written)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.file.flush()
    }
}

impl Drop for StreamedInsert {
    fn drop(&mut self) {
        if let Some(path) = &self.path {
            fs::remove_file(path).ok();
        }
    }
}

/// Per-file hint: (size, mtime_ns, inode) -> content hash. Hints only let
/// us skip re-hashing; any metadata mismatch falls back to hashing bytes,
/// so mtimes never decide correctness.
type Hints = std::collections::HashMap<String, (u64, i64, u64, String)>;

fn stat_key(md: &fs::Metadata) -> (u64, i64, u64) {
    use std::os::unix::fs::MetadataExt;
    (
        md.size(),
        md.mtime() * 1_000_000_000 + md.mtime_nsec(),
        md.ino(),
    )
}

/// Hash a directory tree by *content only*: relative paths + file bytes.
/// mtimes, inode numbers, absolute paths never enter the hash.
pub fn hash_dir(root: &Path) -> Result<String> {
    hash_dir_hinted(root, &Hints::new(), &mut Hints::new())
}

fn hash_dir_hinted(root: &Path, hints: &Hints, fresh: &mut Hints) -> Result<String> {
    fn walk(
        h: &mut Sha256,
        root: &Path,
        rel: &Path,
        hints: &Hints,
        fresh: &mut Hints,
    ) -> Result<()> {
        let dir = root.join(rel);
        let mut names: Vec<std::ffi::OsString> = Vec::new();
        for e in fs::read_dir(&dir).with_context(|| format!("read_dir {}", dir.display()))? {
            names.push(e?.file_name());
        }
        names.sort();
        for name in names {
            let l = name.to_string_lossy().into_owned();
            // Never let build outputs or vcs state into the source hash.
            if rel.as_os_str().is_empty()
                && matches!(l.as_str(), "target" | "dtarget" | "Cargo.lock")
            {
                continue;
            }
            if l == ".git" {
                continue;
            }
            let rel_child = rel.join(&name);
            let p = root.join(&rel_child);
            let md = fs::symlink_metadata(&p)?;
            let rl = rel_child.to_string_lossy().into_owned();
            if md.file_type().is_symlink() {
                h.update(b"L");
                h.update(rl.as_bytes());
                h.update([0]);
                h.update(fs::read_link(&p)?.to_string_lossy().as_bytes());
                h.update([0]);
            } else if md.is_dir() {
                h.update(b"D");
                h.update(rl.as_bytes());
                h.update([0]);
                walk(h, root, &rel_child, hints, fresh)?;
            } else {
                let key = stat_key(&md);
                STAT_FILES.fetch_add(1, Ordering::Relaxed);
                let content = match hints.get(&rl) {
                    Some((sz, mt, ino, hash)) if (*sz, *mt, *ino) == key => hash.clone(),
                    _ => {
                        REHASHED_FILES.fetch_add(1, Ordering::Relaxed);
                        sha256_file(&p)?
                    }
                };
                fresh.insert(rl.clone(), (key.0, key.1, key.2, content.clone()));
                h.update(b"F");
                h.update(rl.as_bytes());
                h.update([0]);
                h.update(content.as_bytes());
                h.update([0]);
            }
        }
        Ok(())
    }
    let mut h = Sha256::new();
    walk(&mut h, root, Path::new(""), hints, fresh)?;
    Ok(hex(&h.finalize()))
}

fn hash_files_hinted(
    root: &Path,
    relative_paths: &[PathBuf],
    hints: &Hints,
    fresh: &mut Hints,
) -> Result<String> {
    let mut relative_paths = relative_paths.to_vec();
    relative_paths.sort();
    relative_paths.dedup();

    let mut hasher = Sha256::new();
    for relative_path in relative_paths {
        if relative_path.is_absolute()
            || relative_path
                .components()
                .any(|component| component == std::path::Component::ParentDir)
        {
            return Err(anyhow!(
                "source path {} is not relative to {}",
                relative_path.display(),
                root.display()
            ));
        }

        let path = root.join(&relative_path);
        let metadata =
            fs::metadata(&path).with_context(|| format!("stat source {}", path.display()))?;
        if !metadata.is_file() {
            return Err(anyhow!("source {} is not a file", path.display()));
        }

        let relative = relative_path.to_string_lossy().into_owned();
        let key = stat_key(&metadata);
        STAT_FILES.fetch_add(1, Ordering::Relaxed);
        let content = match hints.get(&relative) {
            Some((size, modified, inode, hash)) if (*size, *modified, *inode) == key => {
                hash.clone()
            }
            _ => {
                REHASHED_FILES.fetch_add(1, Ordering::Relaxed);
                sha256_file(&path)?
            }
        };
        fresh.insert(relative.clone(), (key.0, key.1, key.2, content.clone()));
        hasher.update(b"F");
        hasher.update(relative.as_bytes());
        hasher.update([0]);
        hasher.update(content.as_bytes());
        hasher.update([0]);
    }

    Ok(hex(&hasher.finalize()))
}

/// Central machine-wide store. All mutations are write-to-temp + atomic
/// rename, so any number of concurrent builds can share it without locks.
pub struct Store {
    pub root: PathBuf,
    /// Canonical machine-independent spelling of the store root, routed
    /// through a symlink at a path that exists on every machine
    /// (default /Users/Shared/corgi). A poor man's bind mount.
    pub alias: Option<PathBuf>,
    counter: AtomicU64,
}

fn setup_alias(alias: &Path, root: &Path) -> Result<()> {
    if let Ok(t) = fs::read_link(alias) {
        if t == *root {
            return Ok(());
        }
    }
    if alias.exists() && !fs::symlink_metadata(alias)?.file_type().is_symlink() {
        return Err(anyhow!("{} exists and is not a symlink", alias.display()));
    }
    let tmp = alias.with_file_name(format!(".corgi-alias-{}", std::process::id()));
    let _ = fs::remove_file(&tmp);
    std::os::unix::fs::symlink(root, &tmp)?;
    fs::rename(&tmp, alias)?; // atomic swap, lock-free like everything else
    Ok(())
}

impl Store {
    pub fn new(root: PathBuf) -> Result<Store> {
        for d in [
            "cache", "pool", "outdirs", "debug", "tmp", "reports", "metrics",
        ] {
            fs::create_dir_all(root.join(d))?;
        }
        // canonicalize so sandbox path rules match kernel-resolved paths
        // (e.g. /tmp/store -> /private/tmp/store)
        let root = root.canonicalize()?;
        let alias = if std::env::var_os("CORGI_NO_ALIAS").is_some() {
            None
        } else {
            let alias_path = std::env::var_os("CORGI_ALIAS")
                .map(PathBuf::from)
                .unwrap_or_else(|| PathBuf::from("/Users/Shared/corgi"));
            if root == alias_path {
                // the store already lives at the canonical path: no alias,
                // no symlink, nothing for realpath() to see through
                return Ok(Store {
                    root,
                    alias: None,
                    counter: AtomicU64::new(0),
                });
            }
            match setup_alias(&alias_path, &root) {
                Ok(()) => Some(alias_path),
                Err(e) => {
                    eprintln!("corgi warning: no canonical store alias ({e}); embedded OUT_DIR paths will be machine-specific");
                    None
                }
            }
        };
        Ok(Store {
            root,
            alias,
            counter: AtomicU64::new(0),
        })
    }

    /// Throttled use-marker for GC (Go's scheme): refresh a file's mtime on
    /// use, at most ~hourly. Purely advisory — a missed touch costs at
    /// worst a premature eviction, which self-heals as a cache miss.
    pub fn touch_used(path: &Path) {
        let Ok(md) = fs::metadata(path) else { return };
        if let Ok(modified) = md.modified() {
            if let Ok(age) = modified.elapsed() {
                if age < std::time::Duration::from_secs(3600) {
                    return;
                }
            }
        }
        // Directories (e.g. source checkouts) can't be opened for
        // append; fall back to a read-only handle, which futimens accepts.
        let opened = fs::File::options()
            .append(true)
            .open(path)
            .or_else(|_| fs::File::open(path));
        if let Ok(f) = opened {
            let _ = f.set_modified(std::time::SystemTime::now());
        }
    }

    pub fn logical_root(&self) -> &Path {
        self.alias.as_deref().unwrap_or(&self.root)
    }

    pub fn tmp_path(&self, tag: &str) -> PathBuf {
        let n = self.counter.fetch_add(1, Ordering::Relaxed);
        let t = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        self.root
            .join("tmp")
            .join(format!("{tag}-{}-{t}-{n}", std::process::id()))
    }

    pub fn cache_path(&self, hash: &str) -> PathBuf {
        self.root.join("cache").join(&hash[..2]).join(hash)
    }

    /// Move a file into the CAS, also reporting whether the bytes contain
    /// `needle` — the location-leak tripwire rides the hashing pass for
    /// free. Concurrent inserts of the same content race benignly: rename
    /// over an identical file is fine.
    pub fn insert_file_scan(&self, path: &Path, needle: &[u8]) -> Result<(String, bool)> {
        let (hash, found) = sha256_file_scan(path, needle)?;
        Ok((self.insert_hashed(path, hash)?, found))
    }

    pub(crate) fn begin_streamed_insert(&self, tag: &str) -> Result<StreamedInsert> {
        let path = self.tmp_path(tag);
        let file = fs::File::create(&path)
            .with_context(|| format!("creating streamed CAS input {}", path.display()))?;
        Ok(StreamedInsert {
            path: Some(path),
            file,
            digest: Sha256::new(),
            bytes: 0,
        })
    }

    pub(crate) fn finish_streamed_insert(
        &self,
        mut pending: StreamedInsert,
    ) -> Result<(String, u64)> {
        pending.flush()?;
        let hash = hex(&std::mem::take(&mut pending.digest).finalize());
        let bytes = pending.bytes;
        let hash = {
            let path = pending
                .path
                .as_deref()
                .context("streamed CAS input missing")?;
            self.insert_hashed(path, hash)?
        };
        pending.path = None;
        Ok((hash, bytes))
    }

    fn insert_hashed(&self, path: &Path, hash: String) -> Result<String> {
        let dest = self.cache_path(&hash);
        if dest.exists() {
            fs::remove_file(path).ok();
            return Ok(hash);
        }
        fs::create_dir_all(dest.parent().unwrap())?;
        match fs::rename(path, &dest) {
            Ok(()) => {}
            Err(_) if dest.exists() => {
                fs::remove_file(path).ok();
            }
            Err(e) => return Err(e).with_context(|| format!("cas insert {}", dest.display())),
        }
        Ok(hash)
    }

    /// Insert in-memory bytes into the CAS (used for cached plan JSON).
    pub fn insert_bytes(&self, data: &[u8]) -> Result<String> {
        let hash = sha256_hex(data);
        let dest = self.cache_path(&hash);
        if !dest.exists() {
            self.write_atomic(&dest, data)?;
        }
        Ok(hash)
    }

    pub fn write_atomic(&self, dest: &Path, data: &[u8]) -> Result<()> {
        fs::create_dir_all(dest.parent().unwrap())?;
        let tmp = self.tmp_path("w");
        fs::write(&tmp, data)?;
        fs::rename(&tmp, dest).with_context(|| format!("atomic write {}", dest.display()))?;
        Ok(())
    }

    pub fn action_path(&self, key: &str) -> PathBuf {
        self.root
            .join("cache")
            .join(&key[..2])
            .join(format!("{key}.json"))
    }

    pub fn load_action(&self, key: &str) -> Option<Vec<u8>> {
        let p = self.action_path(key);
        let data = fs::read(&p).ok()?;
        Store::touch_used(&p);
        Some(data)
    }

    pub fn save_action(&self, key: &str, data: &[u8]) -> Result<()> {
        self.write_atomic(&self.action_path(key), data)
    }

    /// A pool spelling for a produced file: the producing action's key
    /// spliced into the name before the extension, so a pool name only ever
    /// refers to outputs of one action. Rustc uses one source-independent
    /// -Cextra-filename across edits, so produced names alone would re-map to
    /// new content on every edit and concurrent builds would race. The splice
    /// must be the same for an action's rmeta and rlib: rustc's crate locator
    /// groups flavor candidates by the file-name remainder after
    /// lib<name><extra-filename>, and transitive resolution only sees the rlib
    /// when the pair lands in one group.
    pub fn pool_file_name(name: &str, action_key: &str) -> String {
        let key16 = &action_key[..16];
        if name.contains(key16) {
            return name.to_string();
        }
        match name.rsplit_once('.') {
            Some((stem, extension)) => format!("{stem}-{key16}.{extension}"),
            None => format!("{name}-{key16}"),
        }
    }

    /// Where one action's split debug-info objects live. A linked image
    /// records these paths in its debug map, so they are addressed by the
    /// action that produced the image: each compile owns its own directory,
    /// and reclaiming it costs only source-level debugging of a binary that
    /// can be rebuilt.
    pub fn debug_objects_dir(&self, action_key: &str) -> PathBuf {
        self.root.join("debug").join(&action_key[..16])
    }

    /// The same directory as every machine spells it. Paths a compiler bakes
    /// into its output must use this spelling, never the physical one.
    pub fn debug_objects_dir_logical(&self, action_key: &str) -> PathBuf {
        self.logical_root().join("debug").join(&action_key[..16])
    }

    /// Hard-link a debug object back into its action's directory under the
    /// name the debug map records, so a cached image stays debuggable
    /// without recompiling. The directory's mtime doubles as its use marker
    /// for expiry.
    pub fn materialize_debug_object(
        &self,
        action_key: &str,
        file_name: &str,
        hash: &str,
    ) -> Result<()> {
        let dir = self.debug_objects_dir(action_key);
        fs::create_dir_all(&dir)?;
        let dest = dir.join(file_name);
        if fs::symlink_metadata(&dest).is_err() {
            match fs::hard_link(self.cache_path(hash), &dest) {
                Ok(()) => {}
                Err(_) if dest.exists() => {}
                Err(e) => return Err(e).with_context(|| format!("materialize {}", dest.display())),
            }
        }
        Store::touch_used(&dir);
        Ok(())
    }

    /// Hard-link a CAS blob into the pool under its rustc-visible file name.
    /// Names embed the producing action's key (pool_file_name), so an
    /// existing entry with different bytes can only be a nondeterministic
    /// twin from a concurrent run of the same action: interchangeable, so
    /// the first writer wins and an existing name is never re-pointed —
    /// consumers may already hold the path.
    pub fn materialize_pool(
        &self,
        hash: &str,
        file_name: &str,
        executable: bool,
    ) -> Result<PathBuf> {
        let dest = self.root.join("pool").join(file_name);
        if fs::symlink_metadata(&dest).is_ok() {
            return Ok(dest);
        }
        let src = self.cache_path(hash);
        if executable {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = fs::metadata(&src)?.permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&src, perms)?;
        }
        match fs::hard_link(&src, &dest) {
            Ok(()) => {}
            Err(_) if dest.exists() => {}
            Err(e) => return Err(e).with_context(|| format!("materialize {}", dest.display())),
        }
        Ok(dest)
    }

    /// Content hash of a directory with store-persisted caching.
    /// `immutable_key`: registry/git checkouts never change once extracted,
    /// so their hash is computed once ever. Mutable trees use per-file
    /// (size, mtime, inode) hints; changed files are re-hashed by content.
    pub fn hash_dir_cached(&self, root: &Path, immutable_key: Option<&str>) -> Result<String> {
        if let Some(k) = immutable_key {
            let p = self
                .root
                .join("hints")
                .join(format!("{}.txt", sha256_hex(k.as_bytes())));
            if let Ok(h) = fs::read_to_string(&p) {
                let h = h.trim().to_string();
                if h.len() == 64 {
                    IMMUTABLE_HITS.fetch_add(1, Ordering::Relaxed);
                    Store::touch_used(&p);
                    return Ok(h);
                }
            }
            let h = hash_dir(root)?;
            self.write_atomic(&p, h.as_bytes())?;
            return Ok(h);
        }
        let hint_path = self.root.join("hints").join(format!(
            "{}.json",
            sha256_hex(root.display().to_string().as_bytes())
        ));
        let hints: Hints = fs::read(&hint_path)
            .ok()
            .and_then(|b| serde_json::from_slice(&b).ok())
            .unwrap_or_default();
        Store::touch_used(&hint_path);
        let mut fresh = Hints::new();
        HINTED_DIRS.fetch_add(1, Ordering::Relaxed);
        let h = hash_dir_hinted(root, &hints, &mut fresh)?;
        if fresh != hints {
            self.write_atomic(&hint_path, &serde_json::to_vec(&fresh)?)?;
        }
        Ok(h)
    }

    /// Content hash of a selected set of files with store-persisted caching.
    pub fn hash_files_cached(&self, root: &Path, relative_paths: &[PathBuf]) -> Result<String> {
        let hint_path = self.root.join("hints").join(format!(
            "{}.json",
            sha256_hex(format!("files:{}", root.display()).as_bytes())
        ));
        let hints: Hints = fs::read(&hint_path)
            .ok()
            .and_then(|bytes| serde_json::from_slice(&bytes).ok())
            .unwrap_or_default();
        Store::touch_used(&hint_path);
        let mut fresh = Hints::new();
        HINTED_DIRS.fetch_add(1, Ordering::Relaxed);
        let hash = hash_files_hinted(root, relative_paths, &hints, &mut fresh)?;
        if fresh != hints {
            self.write_atomic(&hint_path, &serde_json::to_vec(&fresh)?)?;
        }
        Ok(hash)
    }

    /// Copy a CAS blob out to a destination in the worktree.
    /// Skips the write when the destination already has identical content.
    pub fn export(&self, hash: &str, dest: &Path, executable: bool) -> Result<bool> {
        // A stat hint (size/mtime/inode -> content hash) makes the unchanged
        // case free: re-reading a ~1 GiB binary to decide "already correct"
        // dominated warm zed builds (1.85s of a 2.0s no-op).
        let hint_path = self.root.join("hints").join(format!(
            "{}.json",
            sha256_hex(format!("export:{}", dest.display()).as_bytes())
        ));
        if let Ok(md) = fs::metadata(dest) {
            let key = stat_key(&md);
            let hinted: Option<(u64, i64, u64, String)> = fs::read(&hint_path)
                .ok()
                .and_then(|b| serde_json::from_slice(&b).ok());
            if let Some((size, mtime, inode, hinted_hash)) = hinted {
                if (size, mtime, inode) == key && hinted_hash == hash {
                    return Ok(false);
                }
            }
            EXPORT_CHECK_BYTES.fetch_add(md.len(), Ordering::Relaxed);
            if let Ok(existing) = sha256_file(dest) {
                if existing == hash {
                    self.write_export_hint(&hint_path, dest, hash);
                    return Ok(false);
                }
            }
        }
        fs::create_dir_all(dest.parent().unwrap())?;
        let tmp = dest
            .parent()
            .unwrap()
            .join(format!(".corgi-tmp-{}", std::process::id()));
        fs::copy(self.cache_path(hash), &tmp)?;
        if executable {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = fs::metadata(&tmp)?.permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&tmp, perms)?;
        }
        fs::rename(&tmp, dest)?;
        self.write_export_hint(&hint_path, dest, hash);
        Ok(true)
    }

    /// Best-effort: record the just-verified/just-written destination stat.
    fn write_export_hint(&self, hint_path: &Path, dest: &Path, hash: &str) {
        if let Ok(md) = fs::metadata(dest) {
            let key = stat_key(&md);
            if let Ok(bytes) = serde_json::to_vec(&(key.0, key.1, key.2, hash)) {
                self.write_atomic(hint_path, &bytes).ok();
            }
        }
    }
}

#[cfg(test)]
mod pool_name_tests {
    use super::Store;

    #[test]
    fn action_key_is_spliced_before_the_extension() {
        let key = "1111222233334444ffffffffffffffff";
        assert_eq!(
            Store::pool_file_name("libfoo-aaaabbbbccccdddd.rlib", key),
            "libfoo-aaaabbbbccccdddd-1111222233334444.rlib"
        );
        assert_eq!(
            Store::pool_file_name("build_script_build-aaaabbbbccccdddd", key),
            "build_script_build-aaaabbbbccccdddd-1111222233334444"
        );
    }

    #[test]
    fn names_already_embedding_the_key_pass_through() {
        let key = "aaaabbbbccccddddffffffffffffffff";
        assert_eq!(
            Store::pool_file_name("libfoo-aaaabbbbccccdddd.rlib", key),
            "libfoo-aaaabbbbccccdddd.rlib"
        );
    }

    #[test]
    fn rmeta_and_rlib_of_one_action_share_the_locator_group() {
        // rustc's crate locator groups flavor candidates by the file-name
        // remainder after lib<name><extra-filename>; transitive resolution
        // only sees the rlib when the pair shares that remainder.
        let key = "1111222233334444ffffffffffffffff";
        let rmeta = Store::pool_file_name("libfoo-aaaabbbbccccdddd.rmeta", key);
        let rlib = Store::pool_file_name("libfoo-aaaabbbbccccdddd.rlib", key);
        assert_eq!(
            rmeta.strip_suffix(".rmeta").unwrap(),
            rlib.strip_suffix(".rlib").unwrap()
        );
    }
}

#[cfg(test)]
mod scan_tests {
    use super::sha256_file_scan;

    fn scan(content: &[u8], needle: &[u8]) -> bool {
        let path = std::env::temp_dir().join(format!("corgi-scan-test-{}", std::process::id()));
        std::fs::write(&path, content).unwrap();
        let (_, found) = sha256_file_scan(&path, needle).unwrap();
        std::fs::remove_file(&path).ok();
        found
    }

    #[test]
    fn finds_needle_across_chunk_boundary() {
        // The hasher reads 64 KiB chunks; plant the needle straddling the
        // first boundary so half sits in each read.
        let needle = b"/Users/conrad/worktrees/zed-one";
        let mut content = vec![b'x'; (1 << 16) - 10];
        content.extend_from_slice(needle);
        content.extend_from_slice(&vec![b'y'; 1 << 16]);
        assert!(scan(&content, needle));
        // Same content without the needle is clean.
        let clean = vec![b'x'; (1 << 17) + 21];
        assert!(!scan(&clean, needle));
        // Needle at the very start and very end.
        let mut head = needle.to_vec();
        head.extend_from_slice(&[b'z'; 100]);
        assert!(scan(&head, needle));
        let mut tail = vec![b'z'; (1 << 16) * 2];
        tail.extend_from_slice(needle);
        assert!(scan(&tail, needle));
    }
}