warpline-core 0.2.0

Sandboxed multi-tenant WebAssembly runtime core for warpline: wasmtime Component Model host bindings, epoch CPU and memory caps, tenant-scoped KV, allowlisted outbound HTTP, content-addressed module cache and metering.
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
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
//! Content-addressed module registry shared by `warpline-control` (writer)
//! and `warpline-host` (reader).
//!
//! Layout under `WARPLINE_MODULES_DIR` (default `./modules`):
//!
//! ```text
//! modules/
//!   wasm/{digest}.wasm              -- shared, content-addressed source
//!   cwasm/{digest}-{compat}.cwasm   -- shared, content-addressed (cache.rs)
//!   tenants/{tenant}/{func}         -- pointer file, contents = digest
//! ```
//!
//! One flat `cwasm/` directory means two tenants uploading the same module
//! share the compiled cache entry — see `cache.rs` module docs. The
//! pointer file is the only per-(tenant, func) state; the host re-reads it
//! on every invoke (see [`resolve`]) so a re-upload takes effect
//! immediately, and [`ComponentCache`] is what keeps a warm invoke from
//! re-deserializing the `.cwasm` on every call.
//!
//! The source `wasm/` copy exists because the `.cwasm` cache key folds in
//! `Engine::precompile_compatibility_hash()` (see `cache.rs`): an engine
//! upgrade or config change makes every existing `.cwasm` file unloadable
//! by the new binary, and a `.cwasm` can also simply go missing (deleted,
//! disk issue). Without the source on disk too, every pointer into that
//! stale cache would 500 forever; with it, [`ComponentCache::get_or_load`]
//! recompiles on a miss instead.

use std::collections::HashSet;
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};

use lru::LruCache;
use wasmtime::component::{Component, Linker};
use wasmtime::Engine;

use crate::cache;
use crate::sandbox::HandlerPre;
use crate::types::{valid_name, HostCtx};
use crate::Error;

/// The shared, content-addressed `.cwasm` cache dir under `modules_dir` —
/// what `cache::load_or_compile`/`cache::load_cwasm` read and write.
pub(crate) fn cwasm_dir(modules_dir: &Path) -> PathBuf {
    modules_dir.join("cwasm")
}

/// The shared, content-addressed source-`.wasm` dir under `modules_dir` —
/// see module docs.
pub(crate) fn wasm_dir(modules_dir: &Path) -> PathBuf {
    modules_dir.join("wasm")
}

fn wasm_path(modules_dir: &Path, digest: &str) -> PathBuf {
    wasm_dir(modules_dir).join(format!("{digest}.wasm"))
}

/// Whether the source `.wasm` for `digest` is on disk.
pub(crate) fn wasm_exists(modules_dir: &Path, digest: &str) -> bool {
    cache::is_valid_digest(digest) && wasm_path(modules_dir, digest).is_file()
}

fn pointer_path(modules_dir: &Path, tenant: &str, func: &str) -> Option<PathBuf> {
    if !valid_name(tenant) || !valid_name(func) {
        return None;
    }
    Some(modules_dir.join("tenants").join(tenant).join(func))
}

/// Atomically write the pointer file for `(tenant, func)` so it now
/// resolves to `digest`. Same tempfile-in-same-dir-then-rename pattern as
/// `cache`'s writes, for the same reason: a reader (`read_pointer`) never
/// observes a partial write.
pub(crate) fn write_pointer(
    modules_dir: &Path,
    tenant: &str,
    func: &str,
    digest: &str,
) -> Result<(), Error> {
    if !cache::is_valid_digest(digest) {
        return Err(Error::Corrupt(
            "invalid digest: expected 64 lowercase hex characters".into(),
        ));
    }
    let path = pointer_path(modules_dir, tenant, func)
        .ok_or_else(|| Error::Corrupt("invalid tenant or function name".into()))?;
    let parent = path.parent().expect("pointer_path always has a parent");
    cache::atomic_write(parent, &path, digest.as_bytes())
}

/// Remove the pointer file for `(tenant, func)`; a missing pointer (or an
/// invalid name) is not an error.
pub(crate) fn remove_pointer(modules_dir: &Path, tenant: &str, func: &str) -> Result<(), Error> {
    let Some(path) = pointer_path(modules_dir, tenant, func) else {
        return Ok(());
    };
    match std::fs::remove_file(&path) {
        Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e.into()),
        _ => Ok(()),
    }
}

/// Set `path`'s mtime to now. Errors (including `NotFound`) are returned.
pub(crate) fn touch(path: &Path) -> std::io::Result<()> {
    std::fs::OpenOptions::new()
        .write(true)
        .open(path)?
        .set_modified(SystemTime::now())
}

/// Atomically persist the uploaded source `.wasm` bytes for `digest` under
/// `modules_dir/wasm/` — see module docs. If the content-addressed file
/// already exists (re-upload of identical bytes, or two tenants uploading
/// the same module) it is left alone but its mtime is bumped, so a GC pass
/// racing this upload sees it as fresh and leaves it for the pointer that is
/// about to reference it.
pub(crate) fn write_wasm_source(
    modules_dir: &Path,
    digest: &str,
    wasm_bytes: &[u8],
) -> Result<(), Error> {
    if !cache::is_valid_digest(digest) {
        return Err(Error::Corrupt(
            "invalid digest: expected 64 lowercase hex characters".into(),
        ));
    }
    let dest = wasm_path(modules_dir, digest);
    match touch(&dest) {
        Ok(()) => return Ok(()),
        // Absent (or collected between the check and the write): write it.
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
        Err(e) => return Err(e.into()),
    }
    cache::atomic_write(&wasm_dir(modules_dir), &dest, wasm_bytes)
}

/// Read the pointer file for `(tenant, func)`, returning `Ok(None)` if no
/// function has been uploaded under that name. Validates both the
/// tenant/func names and the file's contents (must be a 64-hex-char
/// digest) before handing anything back — a pointer file is host-trusted
/// (only `write_pointer` writes it), but the digest still ends up in a
/// cache-directory path, so the same shape check `cache::load_cwasm` does
/// applies here too.
pub(crate) fn read_pointer(
    modules_dir: &Path,
    tenant: &str,
    func: &str,
) -> Result<Option<String>, Error> {
    let Some(path) = pointer_path(modules_dir, tenant, func) else {
        return Ok(None);
    };
    let digest = match std::fs::read_to_string(&path) {
        Ok(s) => s,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(e) => return Err(e.into()),
    };
    let digest = digest.trim();
    if !cache::is_valid_digest(digest) {
        return Err(Error::Corrupt(format!(
            "corrupt pointer file {}: not a valid digest",
            path.display()
        )));
    }
    Ok(Some(digest.to_string()))
}

/// One [`ComponentCache`] entry: the pre-instantiated [`HandlerPre`] (built
/// once from the loaded `Component` — see [`ComponentCache::get_or_load`])
/// plus the serialized `.cwasm` byte length it cost to load — the weight
/// the cache bounds its total footprint by (see [`ComponentCache::insert`]).
struct CacheEntry {
    pre: Arc<HandlerPre<HostCtx>>,
    weight: usize,
}

/// Result of [`ComponentCache::lookup`].
pub(crate) enum Lookup {
    NotFound,
    Hit(Arc<HandlerPre<HostCtx>>),
    Miss(String),
}

/// Result of [`ComponentCache::load_existing`].
pub(crate) enum Loaded {
    Ready(Arc<HandlerPre<HostCtx>>),
    NeedsCompile,
}

struct ComponentCacheInner {
    lru: LruCache<String, CacheEntry>,
    total_bytes: usize,
}

/// In-memory LRU of pre-instantiated components keyed by content digest, so a
/// warm invoke (whose pointer resolves to a digest this cache already
/// holds) skips `Component::deserialize` entirely. A miss falls through to
/// [`cache::load_cwasm`] against `modules_dir`'s shared `cwasm/` directory,
/// and (on top of that failing) to recompiling from the shared `wasm/`
/// source directory — see [`Self::get_or_load`].
///
/// The cache owns the `Engine` and `Linker` it loads with, so a cached entry
/// can never have been built against a different pair than the one invoking.
///
/// Bounded by both entry count and total serialized-cwasm bytes: a handful
/// of huge components hitting the byte budget evicts before the count cap
/// would ever be reached, and vice versa for many small ones.
pub(crate) struct ComponentCache {
    engine: Engine,
    linker: Arc<Linker<HostCtx>>,
    modules_dir: PathBuf,
    inner: Mutex<ComponentCacheInner>,
    byte_budget: usize,
}

impl ComponentCache {
    pub(crate) fn new(
        engine: Engine,
        linker: Arc<Linker<HostCtx>>,
        modules_dir: PathBuf,
        cap: usize,
        byte_budget: usize,
    ) -> Self {
        Self {
            engine,
            linker,
            modules_dir,
            inner: Mutex::new(ComponentCacheInner {
                lru: LruCache::new(NonZeroUsize::new(cap.max(1)).unwrap()),
                total_bytes: 0,
            }),
            byte_budget,
        }
    }

    /// Blocking, cheap: read `(tenant, func)`'s pointer and check the LRU.
    /// A [`Lookup::Miss`] means loading needs disk and possibly a compile; the
    /// caller takes a compile permit (asynchronously, never from a blocking
    /// thread) and then calls [`get_or_load`](Self::get_or_load).
    pub(crate) fn lookup(&self, tenant: &str, func: &str) -> Result<Lookup, Error> {
        let Some(digest) = read_pointer(&self.modules_dir, tenant, func)? else {
            return Ok(Lookup::NotFound);
        };
        let hit = self
            .inner
            .lock()
            .unwrap_or_else(|p| p.into_inner())
            .lru
            .get(&digest)
            .map(|e| e.pre.clone());
        Ok(match hit {
            Some(pre) => Lookup::Hit(pre),
            None => Lookup::Miss(digest),
        })
    }

    /// Resolve `digest` to a pre-instantiated [`HandlerPre`], hitting the
    /// LRU first, then the on-disk `.cwasm` cache, then — if that's missing
    /// or fails to deserialize (deleted, corrupt, or invalidated by an
    /// engine/config upgrade — see module docs) — recompiling from the
    /// persisted source `wasm/{digest}.wasm` and re-publishing a fresh
    /// `.cwasm` for next time. Failing to re-publish (read-only or full
    /// disk) is logged and the freshly compiled component is served anyway.
    /// A cache miss additionally pays for `linker.instantiate_pre`'s
    /// import/export type-check exactly once per digest (not per invoke).
    ///
    /// Concurrent misses on the same digest each recompile independently.
    // ponytail: no single-flight dedupe on a cold digest — a burst of
    // concurrent first-invokes for one freshly-uploaded function each pay
    // for their own recompile. Add a per-digest in-flight map if that shows
    // up as real load.
    pub(crate) fn get_or_load(&self, digest: &str) -> Result<Arc<HandlerPre<HostCtx>>, Error> {
        match self.load_existing(digest)? {
            Loaded::Ready(pre) => Ok(pre),
            Loaded::NeedsCompile => self.recompile(digest),
        }
    }

    /// The cheap part of [`get_or_load`](Self::get_or_load), needing no
    /// compile permit: the LRU, then deserialising the on-disk `.cwasm`.
    /// [`Loaded::NeedsCompile`] means the `.cwasm` is missing, corrupt or
    /// built for another engine.
    pub(crate) fn load_existing(&self, digest: &str) -> Result<Loaded, Error> {
        if let Some(hit) = self
            .inner
            .lock()
            .unwrap_or_else(|p| p.into_inner())
            .lru
            .get(digest)
            .map(|e| e.pre.clone())
        {
            return Ok(Loaded::Ready(hit));
        }

        let cwasm_dir = cwasm_dir(&self.modules_dir);
        let component = match cache::load_cwasm(&self.engine, &cwasm_dir, digest) {
            Ok(c) => c,
            Err(e) => {
                tracing::warn!(
                    digest,
                    error = %e,
                    "cwasm cache miss/corrupt, recompiling from stored source"
                );
                return Ok(Loaded::NeedsCompile);
            }
        };
        let weight =
            std::fs::metadata(cwasm_dir.join(cache::cache_file_name(&self.engine, digest)))
                .map(|m| m.len() as usize)
                .unwrap_or(0);
        let pre = Arc::new(crate::sandbox::instantiate_pre(&self.linker, &component)?);
        self.insert(digest, pre.clone(), weight);
        Ok(Loaded::Ready(pre))
    }

    /// Recompile `digest` from the stored source `.wasm` and re-publish a
    /// fresh `.cwasm`. CPU-heavy: callers hold a compile permit.
    pub(crate) fn recompile(&self, digest: &str) -> Result<Arc<HandlerPre<HostCtx>>, Error> {
        let cwasm_dir = cwasm_dir(&self.modules_dir);
        let wasm_bytes = std::fs::read(wasm_path(&self.modules_dir, digest)).map_err(|_| {
            Error::Corrupt(format!(
                "no cwasm and no source wasm on disk for digest {digest}"
            ))
        })?;
        let component = Component::new(&self.engine, &wasm_bytes)?;
        let weight = match cache::persist_cwasm(&component, &self.engine, &cwasm_dir, digest) {
            Ok(len) => len,
            Err(e) => {
                tracing::warn!(
                    digest,
                    error = %e,
                    "could not re-publish recompiled .cwasm; serving from memory"
                );
                wasm_bytes.len()
            }
        };
        let pre = Arc::new(crate::sandbox::instantiate_pre(&self.linker, &component)?);
        self.insert(digest, pre.clone(), weight);
        Ok(pre)
    }

    /// Insert `(digest, pre)` weighted at `weight` bytes, evicting
    /// least-recently-used entries (by count, via `LruCache::put`, and by
    /// `weight` via `pop_lru`) until back under both caps.
    fn insert(&self, digest: &str, pre: Arc<HandlerPre<HostCtx>>, weight: usize) {
        let mut inner = self.inner.lock().unwrap_or_else(|p| p.into_inner());
        if let Some(evicted) = inner
            .lru
            .put(digest.to_string(), CacheEntry { pre, weight })
        {
            inner.total_bytes = inner.total_bytes.saturating_sub(evicted.weight);
        }
        inner.total_bytes += weight;
        while inner.total_bytes > self.byte_budget {
            match inner.lru.pop_lru() {
                Some((_, evicted)) => {
                    inner.total_bytes = inner.total_bytes.saturating_sub(evicted.weight)
                }
                None => break,
            }
        }
    }
}

/// Default grace period for [`Runtime::gc`](crate::Runtime::gc) — an hour is
/// comfortably longer than any single upload's compile-then-persist-then
/// point window could plausibly take, so nothing an in-flight upload has
/// touched in the last hour is ever a GC candidate.
pub const GC_GRACE_PERIOD: Duration = Duration::from_secs(60 * 60);

/// `true` iff `path`'s mtime is at least `grace_period` old. A path this
/// can't `stat` (already gone, race with another remover, permissions)
/// counts as "not eligible" — the caller's own `remove_file` will no-op or
/// surface its own error on this or the next pass, there's nothing to
/// double-check here.
fn older_than(path: &Path, grace_period: Duration) -> bool {
    std::fs::metadata(path)
        .and_then(|m| m.modified())
        .map(|mtime| mtime.elapsed().unwrap_or(Duration::ZERO) >= grace_period)
        .unwrap_or(false)
}

/// Delete `dir`'s direct `tempfile`-crate leftovers (`.tmp*` — see
/// `cache::atomic_write`, the only writer under `modules_dir`) that are
/// older than `grace_period`. A process that crashes between creating a
/// `NamedTempFile` and the rename that publishes it leaves one of these
/// behind forever otherwise; a fresh in-flight write is left alone by the
/// grace period the same way a fresh blob is.
fn sweep_stale_temp_files(dir: &Path, grace_period: Duration) -> usize {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return 0;
    };
    entries
        .flatten()
        .filter(|entry| {
            entry.file_name().to_string_lossy().starts_with(".tmp")
                && older_than(&entry.path(), grace_period)
        })
        .filter(|entry| std::fs::remove_file(entry.path()).is_ok())
        .count()
}

/// Delete every `wasm/{digest}.wasm` blob, `cwasm/{digest}-{compat}.cwasm`
/// blob, and stray `tempfile` leftover under `modules_dir` that's both
/// older than `grace_period` and either unreferenced by any `(tenant,
/// func)` pointer, or — `.cwasm` files only — compiled for an engine other
/// than `engine`'s current `precompile_compatibility_hash()` (it can never
/// be loaded again; `ComponentCache::get_or_load`'s recompile-from-source
/// fallback would just replace it with a fresh one on next access, so there
/// is no reason to keep it around).
///
/// `grace_period` exists so a blob an in-flight upload just wrote (persisted
/// before its pointer, or between an engine upgrade and the next request
/// that would recompile it) is never collected out from under it — pass
/// [`GC_GRACE_PERIOD`] in production, `Duration::ZERO` in a test that wants
/// to observe collection immediately.
///
/// Meant to run once, at startup before serving, so a crash
/// between persisting a blob and writing its pointer (or an old upload that
/// never made it past typecheck, back when the cwasm was written before
/// that check) doesn't leak disk forever. Best-effort: a file that can't be
/// removed is skipped rather than aborting the whole pass, but a pointer that
/// cannot be *read* aborts it before anything is deleted. Returns the number
/// of files removed.
pub(crate) fn gc_unreferenced_blobs(
    modules_dir: &Path,
    engine: &Engine,
    grace_period: Duration,
) -> Result<usize, Error> {
    // Pass 1: learn every referenced digest, deleting nothing. A pointer we
    // cannot read might reference any blob, so *any* read error other than
    // `NotFound` (a vanished entry) aborts the pass before the first delete;
    // treating "unreadable" as "unreferenced" would collect live modules.
    let mut referenced: HashSet<String> = HashSet::new();
    let mut tenant_dirs: Vec<PathBuf> = Vec::new();
    let tenants_dir = modules_dir.join("tenants");
    match std::fs::read_dir(&tenants_dir) {
        Ok(tenants) => {
            for tenant_entry in tenants {
                let tenant_entry = tenant_entry?;
                if !tenant_entry.file_type()?.is_dir() {
                    continue;
                }
                for func_entry in std::fs::read_dir(tenant_entry.path())? {
                    let func_entry = func_entry?;
                    // In-flight `atomic_write` temp files are not pointers.
                    if func_entry.file_name().to_string_lossy().starts_with(".tmp") {
                        continue;
                    }
                    let contents = match std::fs::read_to_string(func_entry.path()) {
                        Ok(c) => c,
                        Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
                        Err(e) => return Err(e.into()),
                    };
                    let digest = contents.trim();
                    if cache::is_valid_digest(digest) {
                        referenced.insert(digest.to_string());
                    }
                }
                tenant_dirs.push(tenant_entry.path());
            }
        }
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
        Err(e) => return Err(e.into()),
    }

    // Pass 2: delete.
    let mut removed = 0usize;
    for dir in &tenant_dirs {
        removed += sweep_stale_temp_files(dir, grace_period);
    }

    let wdir = wasm_dir(modules_dir);
    if wdir.exists() {
        removed += sweep_stale_temp_files(&wdir, grace_period);
        for entry in std::fs::read_dir(&wdir)? {
            let entry = entry?;
            let name = entry.file_name().to_string_lossy().into_owned();
            if let Some(digest) = name.strip_suffix(".wasm") {
                if !referenced.contains(digest)
                    && older_than(&entry.path(), grace_period)
                    && std::fs::remove_file(entry.path()).is_ok()
                {
                    removed += 1;
                }
            }
        }
    }

    let current_compat = cache::compat_hash(engine);
    let cdir = cwasm_dir(modules_dir);
    if cdir.exists() {
        removed += sweep_stale_temp_files(&cdir, grace_period);
        for entry in std::fs::read_dir(&cdir)? {
            let entry = entry?;
            let name = entry.file_name().to_string_lossy().into_owned();
            if let Some(rest) = name.strip_suffix(".cwasm") {
                if let Some((digest, compat)) = rest.split_once('-') {
                    let collectible = !referenced.contains(digest) || compat != current_compat;
                    if collectible
                        && older_than(&entry.path(), grace_period)
                        && std::fs::remove_file(entry.path()).is_ok()
                    {
                        removed += 1;
                    }
                }
            }
        }
    }
    Ok(removed)
}

impl std::fmt::Debug for ComponentCache {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ComponentCache")
            .field("byte_budget", &self.byte_budget)
            .finish_non_exhaustive()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sandbox::{build_engine, build_linker};

    const TEST_GUEST_WASM: &[u8] = include_bytes!(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/tests/fixtures/test_guest.wasm"
    ));

    fn test_cache(engine: &Engine, dir: &Path) -> ComponentCache {
        let linker = Arc::new(build_linker(engine).unwrap());
        ComponentCache::new(engine.clone(), linker, dir.to_path_buf(), 256, usize::MAX)
    }

    #[test]
    fn write_then_read_pointer_roundtrips() {
        let dir = tempfile::tempdir().unwrap();
        let digest = "a".repeat(64);
        write_pointer(dir.path(), "tenant-a", "fn-a", &digest).unwrap();
        assert_eq!(
            read_pointer(dir.path(), "tenant-a", "fn-a").unwrap(),
            Some(digest)
        );
    }

    #[test]
    fn read_pointer_missing_is_none() {
        let dir = tempfile::tempdir().unwrap();
        assert_eq!(read_pointer(dir.path(), "tenant-a", "fn-a").unwrap(), None);
    }

    #[test]
    fn read_pointer_invalid_names_are_none() {
        let dir = tempfile::tempdir().unwrap();
        assert_eq!(read_pointer(dir.path(), "../etc", "fn-a").unwrap(), None);
    }

    #[test]
    fn write_pointer_rejects_bad_digest() {
        let dir = tempfile::tempdir().unwrap();
        assert!(write_pointer(dir.path(), "tenant-a", "fn-a", "not-a-digest").is_err());
    }

    #[test]
    fn re_upload_overwrites_pointer() {
        let dir = tempfile::tempdir().unwrap();
        let d1 = "a".repeat(64);
        let d2 = "b".repeat(64);
        write_pointer(dir.path(), "tenant-a", "fn-a", &d1).unwrap();
        write_pointer(dir.path(), "tenant-a", "fn-a", &d2).unwrap();
        assert_eq!(
            read_pointer(dir.path(), "tenant-a", "fn-a").unwrap(),
            Some(d2)
        );
    }

    /// Finding 1: deleting the `.cwasm` (e.g. after an engine/config change
    /// invalidates the compatibility hash) must not permanently break an
    /// invoke — `get_or_load` should recompile from the persisted source
    /// and re-publish a fresh `.cwasm`.
    #[test]
    fn get_or_load_recompiles_from_source_after_cwasm_deleted() {
        let engine = build_engine().unwrap();
        let dir = tempfile::tempdir().unwrap();
        let cache_obj = test_cache(&engine, dir.path());
        let digest = cache::digest(TEST_GUEST_WASM);

        write_wasm_source(dir.path(), &digest, TEST_GUEST_WASM).unwrap();
        let component = wasmtime::component::Component::new(&engine, TEST_GUEST_WASM).unwrap();
        cache::persist_cwasm(&component, &engine, &cwasm_dir(dir.path()), &digest).unwrap();

        // Simulate the cwasm going missing entirely.
        std::fs::remove_dir_all(cwasm_dir(dir.path())).unwrap();
        assert!(!cwasm_dir(dir.path()).exists());

        let _loaded = cache_obj
            .get_or_load(&digest)
            .expect("should recompile from the persisted source wasm");

        // Recompiling should have re-published a cwasm for next time.
        assert!(cwasm_dir(dir.path())
            .join(cache::cache_file_name(&engine, &digest))
            .exists());
    }

    #[test]
    fn get_or_load_fails_cleanly_with_no_cwasm_and_no_source() {
        let engine = build_engine().unwrap();
        let dir = tempfile::tempdir().unwrap();
        let cache_obj = test_cache(&engine, dir.path());
        assert!(cache_obj.get_or_load(&"c".repeat(64)).is_err());
    }

    #[test]
    fn gc_removes_unreferenced_blobs_but_keeps_referenced() {
        let engine = build_engine().unwrap();
        let dir = tempfile::tempdir().unwrap();
        let kept = "a".repeat(64);
        let orphan = "b".repeat(64);
        write_pointer(dir.path(), "tenant-a", "fn-a", &kept).unwrap();
        std::fs::create_dir_all(wasm_dir(dir.path())).unwrap();
        std::fs::write(wasm_dir(dir.path()).join(format!("{kept}.wasm")), b"x").unwrap();
        std::fs::write(wasm_dir(dir.path()).join(format!("{orphan}.wasm")), b"y").unwrap();

        let removed =
            gc_unreferenced_blobs(dir.path(), &engine, std::time::Duration::ZERO).unwrap();
        assert_eq!(removed, 1);
        assert!(wasm_dir(dir.path()).join(format!("{kept}.wasm")).exists());
        assert!(!wasm_dir(dir.path()).join(format!("{orphan}.wasm")).exists());
    }

    /// Finding 7: an unreferenced blob younger than `grace_period` must
    /// survive the pass — it could be mid-upload, persisted just before its
    /// pointer.
    #[test]
    fn gc_leaves_young_unreferenced_blobs_alone() {
        let engine = build_engine().unwrap();
        let dir = tempfile::tempdir().unwrap();
        let orphan = "b".repeat(64);
        std::fs::create_dir_all(wasm_dir(dir.path())).unwrap();
        std::fs::write(wasm_dir(dir.path()).join(format!("{orphan}.wasm")), b"y").unwrap();

        let removed =
            gc_unreferenced_blobs(dir.path(), &engine, Duration::from_secs(3600)).unwrap();
        assert_eq!(removed, 0);
        assert!(wasm_dir(dir.path()).join(format!("{orphan}.wasm")).exists());
    }

    /// Finding 7: a `.cwasm` compiled for a different engine than the
    /// current compat hash is collected even though its digest is still
    /// referenced — it can never be loaded again, and the recompile-from-
    /// source fallback will just replace it on next access.
    #[test]
    fn gc_removes_cwasm_with_stale_compat_hash_even_if_referenced() {
        let engine = build_engine().unwrap();
        let dir = tempfile::tempdir().unwrap();
        let digest = "c".repeat(64);
        write_pointer(dir.path(), "tenant-a", "fn-a", &digest).unwrap();
        std::fs::create_dir_all(cwasm_dir(dir.path())).unwrap();
        let stale_path = cwasm_dir(dir.path()).join(format!("{digest}-deadbeef.cwasm"));
        std::fs::write(&stale_path, b"stale").unwrap();
        let current_path = cwasm_dir(dir.path()).join(cache::cache_file_name(&engine, &digest));
        std::fs::write(&current_path, b"current").unwrap();

        let removed =
            gc_unreferenced_blobs(dir.path(), &engine, std::time::Duration::ZERO).unwrap();
        assert_eq!(removed, 1);
        assert!(!stale_path.exists());
        assert!(current_path.exists());
    }

    /// Finding 7: a leftover `tempfile` crate temp file (`.tmp*`, see
    /// `cache::atomic_write`) older than the grace period is swept from the
    /// content-addressed dirs.
    #[test]
    fn gc_sweeps_stale_temp_files() {
        let engine = build_engine().unwrap();
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(wasm_dir(dir.path())).unwrap();
        let tmp = wasm_dir(dir.path()).join(".tmpABCDEF");
        std::fs::write(&tmp, b"partial").unwrap();

        let removed =
            gc_unreferenced_blobs(dir.path(), &engine, std::time::Duration::ZERO).unwrap();
        assert_eq!(removed, 1);
        assert!(!tmp.exists());
    }

    #[test]
    fn component_cache_evicts_by_byte_budget() {
        let engine = build_engine().unwrap();
        let linker = Arc::new(build_linker(&engine).unwrap());
        let cache = ComponentCache::new(engine.clone(), linker.clone(), PathBuf::new(), 256, 1);
        let component = wasmtime::component::Component::new(&engine, TEST_GUEST_WASM).unwrap();
        let pre = Arc::new(crate::sandbox::instantiate_pre(&linker, &component).unwrap());
        cache.insert("d1", pre, 100);
        // Weight (100) far exceeds the 1-byte budget, so the entry must
        // have been evicted immediately after insertion.
        assert_eq!(cache.inner.lock().unwrap().total_bytes, 0);
        assert!(cache.inner.lock().unwrap().lru.is_empty());
    }

    /// An unreadable pointer might reference any blob: GC must error out and
    /// delete nothing, not treat it as "unreferenced".
    #[test]
    fn gc_aborts_on_unreadable_pointer_and_deletes_nothing() {
        let engine = build_engine().unwrap();
        let dir = tempfile::tempdir().unwrap();
        let orphan = "b".repeat(64);
        std::fs::create_dir_all(wasm_dir(dir.path())).unwrap();
        let blob = wasm_dir(dir.path()).join(format!("{orphan}.wasm"));
        std::fs::write(&blob, b"y").unwrap();
        // A directory where a pointer file should be: reading it errors with
        // something other than NotFound, whoever runs the test.
        std::fs::create_dir_all(dir.path().join("tenants/tenant-a/fn-a")).unwrap();

        let err = gc_unreferenced_blobs(dir.path(), &engine, Duration::ZERO);
        assert!(err.is_err(), "expected GC to abort, got {err:?}");
        assert!(
            blob.exists(),
            "nothing may be deleted when a pointer is unreadable"
        );
    }

    #[test]
    fn re_upload_of_existing_source_bumps_mtime() {
        let dir = tempfile::tempdir().unwrap();
        let digest = cache::digest(b"x");
        write_wasm_source(dir.path(), &digest, b"x").unwrap();
        let path = wasm_path(dir.path(), &digest);
        let old = SystemTime::now() - Duration::from_secs(7200);
        std::fs::OpenOptions::new()
            .write(true)
            .open(&path)
            .unwrap()
            .set_modified(old)
            .unwrap();

        write_wasm_source(dir.path(), &digest, b"x").unwrap();
        assert!(!older_than(&path, Duration::from_secs(3600)));
    }
}