capsule-runtime 0.11.0

The runtime that executes Murmur capsules. Runs packaged AI agents in isolation, enforcing WASI capability grants on a Wasmtime component sandbox.
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
//! The session's runtime directory: every file the runtime writes for its own use, outside the
//! workdir the capsule's commands can write.
//!
//! ```text
//! ~/.murmur/sessions/                 0700
//! └── <session-id>/                   0700, created at staging, removed when the session ends
//!     ├── .lock                       flock held from creation until removal
//!     ├── runtime/                    $MURMUR_RUNTIME_DIR: MURMUR.md, task.md, murmur.yaml,
//!     │   │                           tools/, plans/ — readable by commands, never writable
//!     │   └── delegations/            <delegation-id>.txt: the whole answer of a sub-capsule
//!     │                               whose answer was cut in the report its parent was handed
//!     ├── sealed-etc/                 the composed root's /etc/passwd and /etc/group
//!     └── harness/<run-id>/           a `transport: process` harness's files
//! ```
//!
//! On the Full tier a Landlock rule grants `runtime/` read-only to every command; on the Sealed
//! tier it is also bound read-only into the composed root at its own path. `sealed-etc/` and
//! `harness/` get no rule, so no command opens them. [`crate::containment::runtime_dir_report`]
//! says per tier what holds.
//!
//! Every runtime read and write beneath the directory goes through a
//! [`crate::workdir_fs::WorkdirRoot`] sharing the session's refusal latch, so a link is refused
//! there exactly as it is in the workdir.

use std::os::fd::OwnedFd;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};

use rustix::fs::{AtFlags, FileType, FlockOperation, Mode, OFlags};

use crate::protected_paths::canonicalize_existing_prefix;
use crate::workdir_fs::WorkdirRoot;

/// The literal segment a reported path carries in place of a real session id, in
/// `--explain-scope` and `session_start.effective_grants`.
///
/// Substituted by the consumer, from `session_start.session_id`. Never expanded here, so the
/// report `--explain-scope` prints before a session exists and the one the trace carries are
/// identical.
pub const SESSION_ID_PLACEHOLDER: &str = "<session-id>";

/// The variable naming the runtime directory in every shell command's, plan shell step's,
/// `murmur:process` program's and `transport: process` harness's environment. Runtime-owned like
/// `HOME`: no manifest, override or host value can set it.
pub const RUNTIME_DIR_ENV: &str = "MURMUR_RUNTIME_DIR";

/// The guest path a `runtime: tool` with no `capabilities.filesystem.scope` finds the runtime
/// directory at, preopened read-only beside its `"."`.
pub const RUNTIME_DIR_GUEST_PATH: &str = "murmur-runtime";

/// Whether an artifact's guest gets the read-only [`RUNTIME_DIR_GUEST_PATH`] preopen: a
/// `runtime: tool` with no `capabilities.filesystem.scope`, which also holds the whole workdir
/// at `"."`. A driver, a hook and a tool confined to a subtree get none.
#[must_use]
pub(crate) fn gets_runtime_dir_preopen(
    runtime: &murmur_artifact::ArtifactRuntime,
    declares_filesystem_scope: bool,
) -> bool {
    *runtime == murmur_artifact::ArtifactRuntime::Tool && !declares_filesystem_scope
}

/// The tool names, sorted, of the artifacts in `installed` whose guest is preopened at
/// [`RUNTIME_DIR_GUEST_PATH`]: the ones [`gets_runtime_dir_preopen`] holds for. `scoped`
/// answers, by tool name, whether the operator's entry declares a `capabilities.filesystem.scope`.
pub(crate) fn runtime_dir_preopened_tools<'a>(
    installed: impl IntoIterator<Item = &'a crate::types::InstalledArtifactSummary>,
    scoped: impl Fn(&str) -> bool,
) -> Vec<String> {
    let mut names: Vec<String> = installed
        .into_iter()
        .map(|artifact| {
            (
                murmur_artifact::tool_name_of(&artifact.name),
                &artifact.runtime,
            )
        })
        .filter(|(name, runtime)| gets_runtime_dir_preopen(runtime, scoped(name)))
        .map(|(name, _)| name.to_string())
        .collect();
    names.sort();
    names.dedup();
    names
}

/// `~/.murmur/sessions`, relative to the murmur home.
pub const SESSIONS_DIR_NAME: &str = "sessions";

/// The directory beneath `<session-id>/` that `$MURMUR_RUNTIME_DIR` names.
pub const RUNTIME_SUBDIR: &str = "runtime";

/// The directory beneath `runtime/` holding `<delegation-id>.txt`, the whole answer of each
/// sub-capsule whose answer was cut at [`crate::delegation_plane::MAX_OUTPUT_BYTES`] in what the
/// parent was handed. Written only by [`crate::delegation::hand_over_answer`], and only for a cut
/// answer.
pub const DELEGATIONS_SUBDIR: &str = "delegations";

/// The directory beneath `<session-id>/` holding the composed root's synthetic `/etc` files.
pub const SEALED_ETC_SUBDIR: &str = "sealed-etc";

/// The directory beneath `<session-id>/` holding one `<run-id>/` per `transport: process`
/// harness run.
pub const HARNESS_SUBDIR: &str = "harness";

/// The lock file in `<session-id>/`, `flock`ed by the live session.
pub const LOCK_FILE_NAME: &str = ".lock";

/// How old a session directory nobody holds must be before staging removes it: long enough to
/// cover the instant between a directory's creation and its lock.
pub const STALE_AFTER: Duration = Duration::from_secs(60);

/// The mode every directory beneath `~/.murmur/sessions` is created at.
pub(crate) const PRIVATE_DIR_MODE: u32 = crate::murmur_home::MURMUR_HOME_DIR_MODE;

/// Where the runtime directory and the records directory lie relative to a workdir, decided
/// without creating anything.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RuntimeDirPlacement {
    /// `~/.murmur/sessions`, as `HOME` names it.
    pub sessions_dir: PathBuf,
    /// The records group of [`Self::workdir`], [`crate::records_dir::records_group`]'s answer:
    /// every session of this workdir root keeps its records directory in it.
    pub records_group: PathBuf,
    /// The workdir root it was compared with: `--workdir`, else `<manifest-dir>/workdir`.
    pub workdir: PathBuf,
    /// Whether `sessions_dir` lies inside `workdir`, both resolved through their nearest
    /// existing ancestor. When it does, the capsule's commands can change the runtime directory.
    pub sessions_inside_workdir: bool,
    /// Whether this session's records directory lies inside `workdir`, resolved the same way.
    pub records_inside_workdir: bool,
}

impl RuntimeDirPlacement {
    /// Whether either directory lies inside the workdir: the capsule's commands can change a
    /// directory the runtime keeps out of their reach.
    #[must_use]
    pub fn inside_workdir(&self) -> bool {
        self.sessions_inside_workdir || self.records_inside_workdir
    }

    /// `Some(self)` when the runtime directory or the records directory lies inside the workdir.
    #[must_use]
    pub fn inside(&self) -> Option<&Self> {
        self.inside_workdir().then_some(self)
    }

    /// A runtime directory and a records group under `/home/test` and a workdir under `/work`,
    /// apart, for a test that is not about where the directories lie.
    #[cfg(test)]
    pub(crate) fn outside_for_test() -> Self {
        Self {
            sessions_dir: PathBuf::from("/home/test/.murmur/sessions"),
            records_group: PathBuf::from("/home/test/.murmur/records/work"),
            workdir: PathBuf::from("/work"),
            sessions_inside_workdir: false,
            records_inside_workdir: false,
        }
    }

    /// The runtime directory's path with the literal `<session-id>` segment, the form
    /// `--explain-scope` and `session_start.effective_grants` both print.
    #[must_use]
    pub fn report_path(&self) -> String {
        self.sessions_dir
            .join(SESSION_ID_PLACEHOLDER)
            .join(RUNTIME_SUBDIR)
            .display()
            .to_string()
    }

    /// The records directory's path with the literal `<session-id>` segment, on
    /// [`Self::report_path`]'s terms.
    #[must_use]
    pub fn records_report_path(&self) -> String {
        self.records_group
            .join(SESSION_ID_PLACEHOLDER)
            .display()
            .to_string()
    }

    /// The directory, or both, that lies inside the workdir, and what the capsule's commands can
    /// therefore change, as `(subject with its verb, what can be changed)`.
    ///
    /// Called only for a placement [`Self::inside_workdir`] holds for, so a placement with
    /// neither directory inside reads as the records directory alone.
    fn inside_subject(&self) -> (String, &'static str) {
        debug_assert!(self.inside_workdir());
        let runtime = format!("the runtime directory {}", self.sessions_dir.display());
        let records = format!("the records directory {}", self.records_report_path());
        match (self.sessions_inside_workdir, self.records_inside_workdir) {
            (true, true) => (
                format!("{runtime} and {records} lie"),
                "the files the runtime owns — task.md, MURMUR.md, plans and installed tools — \
                 and the records it keeps about the session — its trace, logs, eval scores and \
                 delegation outcome",
            ),
            (true, false) => (
                format!("{runtime} lies"),
                "the files the runtime owns — task.md, MURMUR.md, plans and installed tools",
            ),
            (false, _) => (
                format!("{records} lies"),
                "the records the runtime keeps about the session — its trace, logs, eval scores \
                 and delegation outcome",
            ),
        }
    }

    /// `E-CAP-003`'s reason for a placement inside the workdir.
    #[must_use]
    pub(crate) fn inside_shortfall(&self) -> String {
        let (subject, changes) = self.inside_subject();
        format!(
            "{subject} inside the workdir {}, so this capsule's commands can change {changes}, \
             and no host can back a class above advisory for it; run with a --workdir that does \
             not contain ~/.murmur",
            self.workdir.display()
        )
    }
}

/// The `W-SEC-033` line for a runtime or records directory inside the workdir, without a prefix:
/// shared by `stage_session` and `mur doctor` so the two cannot word it differently.
#[must_use]
pub fn runtime_dir_inside_warning(placement: &RuntimeDirPlacement) -> String {
    use murmur_artifact::{security_warning_link, W_SEC_033};
    let (subject, changes) = placement.inside_subject();
    format!(
        "warning[{W_SEC_033}]: {subject} inside the workdir {}, so this capsule's commands can \
         change {changes}; run with a --workdir that does not contain ~/.murmur ({})",
        placement.workdir.display(),
        security_warning_link(W_SEC_033)
    )
}

/// The `sessions/` directory every runtime directory on this host is created in,
/// `~/.murmur/sessions`, decided without creating anything. A `HOME` that cannot hold it is
/// refused, never replaced by another location.
pub fn sessions_dir() -> Result<PathBuf, crate::murmur_home::HomeUnusable> {
    Ok(crate::murmur_home::usable_murmur_home()?.join(SESSIONS_DIR_NAME))
}

/// Where the runtime directory and the records directory would lie relative to `workdir`, the
/// session's workdir root. Creates nothing.
///
/// Each path is resolved by canonicalizing its nearest existing ancestor and appending the rest,
/// so a workdir that does not exist yet, or a home with no `.murmur` in it yet, is judged by
/// where it would be created.
pub fn runtime_dir_placement(
    workdir: &Path,
) -> Result<RuntimeDirPlacement, crate::murmur_home::HomeUnusable> {
    let workdir = std::path::absolute(workdir).unwrap_or_else(|_| workdir.to_path_buf());
    let sessions_dir = sessions_dir()?;
    let records_group = crate::records_dir::records_group(&workdir)?;
    let canonical_workdir = canonicalize_existing_prefix(&workdir);
    let sessions_inside_workdir =
        canonicalize_existing_prefix(&sessions_dir).starts_with(&canonical_workdir);
    let records_inside_workdir =
        canonicalize_existing_prefix(&records_group).starts_with(&canonical_workdir);
    Ok(RuntimeDirPlacement {
        sessions_dir,
        records_group,
        workdir,
        sessions_inside_workdir,
        records_inside_workdir,
    })
}

/// The three held directories beneath one session's `<session-id>/`, and the session's records
/// directory beside them, each sharing the session's refusal latch: every directory outside the
/// workdir a command is told of or a sealed root is composed from. Cheap to clone.
#[derive(Clone, Debug)]
pub struct RuntimeDirRoots {
    /// `runtime/`, which `$MURMUR_RUNTIME_DIR` names.
    pub runtime: WorkdirRoot,
    /// `sealed-etc/`.
    pub sealed_etc: WorkdirRoot,
    /// `harness/`.
    pub harness: WorkdirRoot,
    /// The session's records directory, which `$MURMUR_RECORDS_DIR` names, given the same
    /// read-only rule and bind as `runtime/`. `None` for roots held outside a session, where no
    /// records directory exists.
    pub records: Option<WorkdirRoot>,
    /// The session's temporary folder, `tmp/` in its records directory: `$TMPDIR` in every
    /// command, and `/tmp` wherever the session maps it ([`crate::temp_dir`]). `None` for roots
    /// held outside a session, whose commands are told of no temporary folder.
    pub temp: Option<WorkdirRoot>,
    /// The session's home folder, `home/` in its records directory: `$HOME` in every command,
    /// granted exactly as [`Self::temp`] is. `None` for roots held outside a session, where no
    /// command runs.
    pub home: Option<WorkdirRoot>,
}

impl RuntimeDirRoots {
    /// The three roots of a session's runtime directory, for a caller that holds them itself —
    /// a test, or a plan run outside a session. No records directory.
    #[must_use]
    pub fn new(runtime: WorkdirRoot, sealed_etc: WorkdirRoot, harness: WorkdirRoot) -> Self {
        Self {
            runtime,
            sealed_etc,
            harness,
            records: None,
            temp: None,
            home: None,
        }
    }

    /// These roots with `records` as the session's records directory.
    #[must_use]
    pub fn with_records(mut self, records: WorkdirRoot) -> Self {
        self.records = Some(records);
        self
    }

    /// These roots with `temp` as the session's temporary folder.
    #[must_use]
    pub fn with_temp(mut self, temp: WorkdirRoot) -> Self {
        self.temp = Some(temp);
        self
    }

    /// These roots with `home` as the session's home folder.
    #[must_use]
    pub fn with_home(mut self, home: WorkdirRoot) -> Self {
        self.home = Some(home);
        self
    }
}

/// One session's runtime directory, from its creation at staging to its removal when the value
/// is dropped.
pub struct RuntimeDir {
    session_id: String,
    /// `~/.murmur/sessions`, held; the session directory is removed through it.
    sessions: WorkdirRoot,
    /// `<session-id>/.lock`, `flock`ed for as long as this value lives.
    _lock: OwnedFd,
    roots: RuntimeDirRoots,
}

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

impl RuntimeDir {
    /// Creates `<sessions_path>/<session_id>/`, locks it, sweeps stale siblings and creates
    /// `runtime/`, `sealed-etc/` and `harness/` beneath it at `0700`. Every refusal beneath the
    /// three lands on `latch_of`'s latch and names its path absolutely.
    ///
    /// `sessions_path` is [`sessions_dir`]'s answer, the one the launch was judged against. The
    /// murmur home is held through [`crate::murmur_home::ensure_murmur_home`] first.
    pub fn create(
        sessions_path: &Path,
        session_id: &str,
        latch_of: &WorkdirRoot,
    ) -> Result<Self, String> {
        let located = |path: &Path, reason: String| format!("{}: {reason}", path.display());
        let home = crate::murmur_home::ensure_murmur_home()?;
        if sessions_path.parent() != Some(home.as_path()) {
            return Err(located(
                sessions_path,
                format!("is not beneath {}", home.display()),
            ));
        }
        let sessions_path = sessions_path.to_path_buf();
        crate::murmur_home::hold_private_dir(&sessions_path)
            .map_err(|reason| located(&sessions_path, reason))?;
        let sessions = WorkdirRoot::open(&sessions_path).map_err(|error| error.to_string())?;
        sweep_stale(&sessions, SystemTime::now());

        let session_path = sessions_path.join(session_id);
        crate::murmur_home::hold_private_dir(&session_path)
            .map_err(|reason| located(&session_path, reason))?;
        let session_dir_root = sessions
            .open_beneath(session_id)
            .map_err(|error| error.to_string())?
            .ok_or_else(|| located(&session_path, "vanished after its creation".to_string()))?;
        let lock = take_lock(&session_dir_root, &session_path)?;
        for sub in [RUNTIME_SUBDIR, SEALED_ETC_SUBDIR, HARNESS_SUBDIR] {
            session_dir_root
                .create_dir_all(sub, PRIVATE_DIR_MODE)
                .map_err(|error| error.to_string())?;
        }
        let open = |sub: &str| {
            WorkdirRoot::open_sharing_latch_absolute(&session_path.join(sub), latch_of)
                .map_err(|error| error.to_string())
        };
        Ok(Self {
            session_id: session_id.to_string(),
            sessions,
            _lock: lock,
            roots: RuntimeDirRoots::new(
                open(RUNTIME_SUBDIR)?,
                open(SEALED_ETC_SUBDIR)?,
                open(HARNESS_SUBDIR)?,
            ),
        })
    }

    /// `runtime/`'s absolute path, the value of `$MURMUR_RUNTIME_DIR`.
    #[must_use]
    pub fn path(&self) -> &Path {
        self.roots.runtime.path()
    }

    /// The three held directories.
    #[must_use]
    pub fn roots(&self) -> &RuntimeDirRoots {
        &self.roots
    }
}

impl Drop for RuntimeDir {
    /// Removes `<session-id>/` by descriptor from the held `sessions/`, before the lock is
    /// released, so no sweep can race the removal.
    fn drop(&mut self) {
        if let Err(error) = self.sessions.remove_dir_all(&self.session_id) {
            if !error.is_not_found() {
                crate::runtime_err!(
                    "[capsule-runtime] warning: failed to remove the runtime directory {}: {error}",
                    self.sessions.host_path(&self.session_id).display()
                );
            }
        }
    }
}

/// The session's [`RuntimeDir`], shared between `launch` and the session's `ShellEnforcement`
/// so the process exits that run no destructor can still remove it. Cheap to clone; every clone
/// removes the one directory.
#[derive(Clone, Default)]
pub(crate) struct SharedRuntimeDir(Arc<Mutex<Option<RuntimeDir>>>);

impl std::fmt::Debug for SharedRuntimeDir {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("SharedRuntimeDir")
    }
}

impl SharedRuntimeDir {
    pub(crate) fn new(dir: Option<RuntimeDir>) -> Self {
        Self(Arc::new(Mutex::new(dir)))
    }

    /// Removes the runtime directory now. Later calls do nothing.
    pub(crate) fn remove(&self) {
        let taken = self
            .0
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .take();
        drop(taken);
    }

    /// A guard that calls [`Self::remove`] when dropped, whatever other clones still exist.
    pub(crate) fn remove_on_drop(&self) -> RemoveRuntimeDirOnDrop {
        RemoveRuntimeDirOnDrop(self.clone())
    }
}

/// Removes a [`SharedRuntimeDir`] when dropped.
pub(crate) struct RemoveRuntimeDirOnDrop(SharedRuntimeDir);

impl Drop for RemoveRuntimeDirOnDrop {
    fn drop(&mut self) {
        self.0.remove();
    }
}

/// Opens (creating it at `0600`) and exclusively locks `<session>/.lock`, following no link.
fn take_lock(session_dir_root: &WorkdirRoot, session_path: &Path) -> Result<OwnedFd, String> {
    let lock_path = session_path.join(LOCK_FILE_NAME);
    let dir = session_dir_root
        .dir_fd()
        .map_err(|error| format!("{}: {error}", session_path.display()))?;
    let fd = rustix::fs::openat(
        &dir,
        LOCK_FILE_NAME,
        OFlags::RDWR | OFlags::CREATE | OFlags::NOFOLLOW | OFlags::CLOEXEC,
        Mode::from_raw_mode(crate::murmur_home::PRIVATE_FILE_MODE as rustix::fs::RawMode),
    )
    .map_err(|errno| format!("{}: {}", lock_path.display(), std::io::Error::from(errno)))?;
    rustix::fs::flock(&fd, FlockOperation::NonBlockingLockExclusive)
        .map_err(|errno| format!("{}: {}", lock_path.display(), std::io::Error::from(errno)))?;
    Ok(fd)
}

/// Removes every entry of `sessions` no live session holds and older than [`STALE_AFTER`]: a
/// directory whose `.lock` can be taken and was last modified before then, and any entry with no
/// `.lock` last modified before then. A live session's lock is never free, so its directory is
/// never removed.
fn sweep_stale(sessions: &WorkdirRoot, now: SystemTime) {
    let Ok(Some(entries)) = sessions.list_dir("") else {
        return;
    };
    let Ok(dir) = sessions.dir_fd() else {
        return;
    };
    let older_than_floor = |stat: &rustix::fs::Stat| {
        let modified = SystemTime::UNIX_EPOCH + Duration::from_secs(stat.st_mtime.max(0) as u64);
        now.duration_since(modified)
            .is_ok_and(|age| age > STALE_AFTER)
    };
    for (name, _) in entries {
        let Ok(entry_stat) = rustix::fs::statat(&dir, name.as_str(), AtFlags::SYMLINK_NOFOLLOW)
        else {
            continue;
        };
        if FileType::from_raw_mode(entry_stat.st_mode) != FileType::Directory {
            if older_than_floor(&entry_stat) {
                let _ = sessions.remove_file(&name);
            }
            continue;
        }
        let lock = rustix::fs::openat(&dir, name.as_str(), DIR_NOFOLLOW, Mode::empty()).and_then(
            |session| {
                rustix::fs::openat(
                    &session,
                    LOCK_FILE_NAME,
                    OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::CLOEXEC | OFlags::NONBLOCK,
                    Mode::empty(),
                )
            },
        );
        match lock {
            Ok(lock) => {
                let free = rustix::fs::flock(&lock, FlockOperation::NonBlockingLockExclusive);
                let stale = rustix::fs::fstat(&lock).is_ok_and(|stat| older_than_floor(&stat));
                if free.is_ok() && stale {
                    let _ = sessions.remove_dir_all(&name);
                }
            }
            Err(rustix::io::Errno::NOENT) if older_than_floor(&entry_stat) => {
                let _ = sessions.remove_dir_all(&name);
            }
            Err(_) => {}
        }
    }
}

const DIR_NOFOLLOW: OFlags = OFlags::RDONLY
    .union(OFlags::DIRECTORY)
    .union(OFlags::NOFOLLOW)
    .union(OFlags::CLOEXEC);

#[cfg(test)]
mod tests {
    use super::*;
    use std::os::unix::fs::PermissionsExt;

    fn set_mtime(path: &Path, age: Duration) {
        let file = std::fs::File::open(path).unwrap();
        file.set_modified(SystemTime::now() - age).unwrap();
    }

    #[test]
    fn runtime_dir_sweep_removes_unlocked_stale_and_keeps_locked_and_recent() {
        let temp = tempfile::tempdir().unwrap();
        let sessions = temp.path();
        let make = |name: &str, age: Duration| {
            let dir = sessions.join(name);
            std::fs::create_dir_all(dir.join("runtime")).unwrap();
            std::fs::write(dir.join(LOCK_FILE_NAME), b"").unwrap();
            set_mtime(&dir.join(LOCK_FILE_NAME), age);
            dir
        };
        let stale = make("ses_stale", Duration::from_secs(61));
        let locked = make("ses_locked", Duration::from_secs(61));
        let recent = make("ses_recent", Duration::from_secs(10));
        let unlocked_dir = sessions.join("ses_nolock");
        std::fs::create_dir_all(&unlocked_dir).unwrap();
        set_mtime(&unlocked_dir, Duration::from_secs(61));

        let held = std::fs::File::open(locked.join(LOCK_FILE_NAME)).unwrap();
        rustix::fs::flock(&held, FlockOperation::NonBlockingLockExclusive).unwrap();

        let root = WorkdirRoot::open(sessions).unwrap();
        sweep_stale(&root, SystemTime::now());

        assert!(
            !stale.exists(),
            "an unlocked 61-second-old directory is removed"
        );
        assert!(
            !unlocked_dir.exists(),
            "an old directory with no lock is removed"
        );
        assert!(locked.exists(), "a locked directory is kept");
        assert!(recent.exists(), "a 10-second-old directory is kept");
        drop(held);
    }

    #[test]
    fn runtime_dir_placement_resolves_through_the_nearest_existing_ancestor() {
        let temp = tempfile::tempdir().unwrap();
        let real = temp.path().join("real");
        std::fs::create_dir_all(&real).unwrap();
        let link = temp.path().join("link");
        std::os::unix::fs::symlink(&real, &link).unwrap();

        let resolved = canonicalize_existing_prefix(&link.join("not-yet").join("deeper"));
        assert_eq!(
            resolved,
            real.canonicalize().unwrap().join("not-yet").join("deeper")
        );
        assert!(canonicalize_existing_prefix(&link.join(".murmur/sessions"))
            .starts_with(canonicalize_existing_prefix(&real)));
    }

    /// A workdir root that holds the murmur home holds both directories; one that holds
    /// `~/.murmur/records` alone holds the records directory alone; one elsewhere holds neither.
    #[test]
    fn runtime_dir_placement_judges_the_records_directory_too() {
        let home = tempfile::tempdir().unwrap();
        crate::murmur_home::run_with_home(
            "runtime_dir::tests::runtime_dir_placement_records_inner",
            home.path(),
        );
    }

    #[test]
    #[ignore = "run by runtime_dir_placement_judges_the_records_directory_too"]
    fn runtime_dir_placement_records_inner() {
        if !crate::murmur_home::in_scratch_home() {
            return;
        }
        let home = PathBuf::from(std::env::var_os("HOME").unwrap());
        let both = runtime_dir_placement(&home).unwrap();
        assert!(both.sessions_inside_workdir && both.records_inside_workdir);
        assert!(both.inside_workdir());
        assert!(runtime_dir_inside_warning(&both).contains("the records directory"));

        let records = home
            .join(".murmur")
            .join(crate::records_dir::RECORDS_DIR_NAME);
        let records_only = runtime_dir_placement(&records).unwrap();
        assert!(!records_only.sessions_inside_workdir);
        assert!(records_only.records_inside_workdir && records_only.inside_workdir());
        let reason = records_only.inside_shortfall();
        assert!(reason.starts_with("the records directory "), "{reason}");
        assert!(reason.contains("run with a --workdir that does not contain ~/.murmur"));

        let elsewhere = runtime_dir_placement(&home.join("project/workdir")).unwrap();
        assert!(!elsewhere.inside_workdir());
        assert_eq!(
            elsewhere.records_report_path(),
            crate::records_dir::records_group(&home.join("project/workdir"))
                .unwrap()
                .join("<session-id>")
                .display()
                .to_string()
        );
        assert!(!home.join(".murmur").exists(), "judging creates nothing");
    }

    #[test]
    fn runtime_dir_lock_is_exclusive_across_open_file_descriptions() {
        let temp = tempfile::tempdir().unwrap();
        let session = temp.path().join("ses_x");
        std::fs::create_dir_all(&session).unwrap();
        let root = WorkdirRoot::open(&session).unwrap();
        let held = take_lock(&root, &session).unwrap();
        let mode = std::fs::metadata(session.join(LOCK_FILE_NAME))
            .unwrap()
            .permissions()
            .mode()
            & 0o777;
        assert_eq!(mode & 0o077, 0, "the lock file is owner-only");
        assert!(
            take_lock(&root, &session).is_err(),
            "a second lock is refused"
        );
        drop(held);
        assert!(take_lock(&root, &session).is_ok());
    }
}