Skip to main content

runnerd_core/
config.rs

1//! `runnerd.toml`: the repositories to serve, the runner profiles they use, the shared caches and the host
2//! budget they must fit in. Builds without the `host` feature, so clients can read a config; its only I/O is
3//! reading the file and asking `systemctl` for a slice's `MemoryMax`. It never talks to Docker or GitHub.
4
5use std::collections::BTreeMap;
6use std::fmt;
7use std::net::{Ipv4Addr, Ipv6Addr};
8use std::path::{Path, PathBuf};
9use std::process::Command;
10use std::str::FromStr;
11use std::time::Duration;
12
13use serde::{Deserialize, Deserializer, de};
14
15use crate::api;
16
17mod budget;
18
19pub use budget::{BudgetBlock, CheckBudget, Remainder, Verdict};
20
21/// A parsed `runnerd.toml`. `validate` checks it against the host's budget.
22#[derive(Debug, Deserialize)]
23#[serde(deny_unknown_fields)]
24pub struct Config {
25    /// The host's budget and isolation.
26    pub host: Host,
27    /// The caches runners share.
28    pub caches: Caches,
29    /// Runner profiles by name.
30    pub profiles: BTreeMap<String, Profile>,
31    /// The repositories to serve, one pool per entry: a repository named by several entries is served by each of
32    /// their profiles, a job by the one whose labels match it (ADR 0058).
33    pub repositories: Vec<Repository>,
34    /// What is kept of past jobs.
35    #[serde(default)]
36    pub history: History,
37    /// How much the records of failed jobs may keep.
38    #[serde(default)]
39    pub records: Records,
40    /// How sizing advice reads the job history.
41    #[serde(default)]
42    pub sizing: Sizing,
43    /// Who may use the dashboard through another address.
44    #[serde(default)]
45    pub web: Web,
46    /// Where the daemon sends notifications; none by default.
47    #[serde(default)]
48    pub notify: Notify,
49}
50
51/// `[host]`: where runner containers run and how they are isolated.
52#[derive(Debug, Deserialize)]
53#[serde(deny_unknown_fields)]
54pub struct Host {
55    /// The systemd slice every runner container is placed in; its `MemoryMax` is the budget.
56    pub slice: String,
57    /// How runner containers are kept from the host.
58    pub isolation: Isolation,
59    /// Must be set for `isolation = "unsafe-dind"`, so a copied config cannot turn it on silently.
60    #[serde(default)]
61    pub allow_unsafe: bool,
62    /// How often GitHub is asked for queued jobs.
63    #[serde(default = "default_poll_interval", deserialize_with = "duration")]
64    pub poll_interval: Duration,
65    /// What a runner must fit in before it starts. Default `caps`.
66    #[serde(default)]
67    pub admission: Admission,
68    /// Set, a runner also starts only when its predicted CPU fits the slice's CPUs times this; absent, CPU is not
69    /// admitted.
70    #[serde(default)]
71    pub cpu_overcommit: Option<f64>,
72    /// Set, the most memory the host can grow to, for a VM whose hypervisor adds memory as the guest asks for it:
73    /// admission counts what it can still grow by past `MemTotal` as available, and keeps the margin of this
74    /// (ADR 0084). Absent, or not above `MemTotal`, the host is the memory it has now.
75    #[serde(default)]
76    pub memory_ceiling: Option<ByteSize>,
77    /// Under measured admission, whether the budget counts what runners reserve as jobs that do not all peak at once,
78    /// up to a bounded credit (ADR 0099). Default on; off, it adds every runner's worst case.
79    #[serde(default = "default_pooled_count")]
80    pub pooled_count: bool,
81}
82
83impl Host {
84    /// Refuses a `cpu_overcommit` that is not above 0 and at most `MAX_CPU_OVERCOMMIT`.
85    fn check_cpu_overcommit(&self) -> Result<(), ConfigError> {
86        match self.cpu_overcommit {
87            Some(value) if !(value > 0.0 && value <= MAX_CPU_OVERCOMMIT) => {
88                Err(ConfigError::CpuOvercommit { value })
89            }
90            _ => Ok(()),
91        }
92    }
93}
94
95/// The largest `cpu_overcommit` the config check takes.
96pub const MAX_CPU_OVERCOMMIT: f64 = 16.0;
97
98/// `admission` under `[host]`: what a new runner must fit in the slice's `MemoryMax` beside the running ones.
99#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
100#[serde(rename_all = "kebab-case")]
101pub enum Admission {
102    /// Every runner's memory cap: `slots × memory` over all profiles, with the mirror, must fit `MemoryMax`.
103    #[default]
104    Caps,
105    /// Every runner's predicted peak, from the job history; `slots` only bounds how many run at once, and one
106    /// runner of each profile with the mirror must fit `MemoryMax`. Nothing starts while the slice is at 90% of it.
107    Measured,
108}
109
110impl Admission {
111    /// The policy as the API names it.
112    pub fn policy(self) -> api::AdmissionPolicy {
113        match self {
114            Admission::Caps => api::AdmissionPolicy::Caps,
115            Admission::Measured => api::AdmissionPolicy::Measured,
116        }
117    }
118}
119
120fn default_pooled_count() -> bool {
121    true
122}
123
124fn default_poll_interval() -> Duration {
125    Duration::from_secs(15)
126}
127
128/// How runner containers are kept from the host: `isolation` under `[host]`.
129#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
130#[serde(rename_all = "kebab-case")]
131pub enum Isolation {
132    /// Sysbox: Docker inside an unprivileged container.
133    Sysbox,
134    /// Privileged Docker-in-Docker: a job can take over the host.
135    UnsafeDind,
136}
137
138/// `[caches]`: what runners share and keep between jobs.
139#[derive(Debug, Clone, Deserialize)]
140#[serde(deny_unknown_fields)]
141pub struct Caches {
142    /// The Docker Hub mirror.
143    pub mirror: Mirror,
144    /// The shared pnpm store.
145    pub pnpm: CacheLimit,
146    /// The shared Maven repository.
147    pub maven: CacheLimit,
148    /// When a profile has no job running and its slot 1 changed its layers by at least this much, slot 1's
149    /// layers become the profile's new snapshot.
150    #[serde(default = "default_reseed_at")]
151    pub reseed_at: ByteSize,
152    /// A reseed that would make a profile's snapshot larger than this starts it over from slot 1's changes alone,
153    /// or from the image when those are larger too.
154    #[serde(default = "default_snapshot_max")]
155    pub snapshot_max: ByteSize,
156    /// The build cache every runner shares.
157    #[serde(default)]
158    pub build: BuildCache,
159}
160
161/// `[caches.build]`: the build cache every runner shares (ADR 0052): Gradle's, and the other tools' the runner image
162/// points at it. Every field has a default.
163#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
164#[serde(deny_unknown_fields, default)]
165pub struct BuildCache {
166    /// Whether runners get it. Default true.
167    pub enabled: bool,
168    /// Above this size, the entries used least recently go when no job runs. Default 10G.
169    pub max_size: ByteSize,
170    /// The branches whose pushes, scheduled and manually started runs may write to it besides each repository's
171    /// default branch, which always may. Every other job only reads. Default empty.
172    pub branches: Vec<String>,
173    /// Where its entries are kept; default `$XDG_CACHE_HOME/runnerd/build-cache`.
174    pub directory: Option<PathBuf>,
175    /// Whether it also serves GitHub Actions' cache, for `actions/cache`, the `setup-*` actions' caches and
176    /// BuildKit's `type=gha`, in place of GitHub's (ADR 0082). Default true.
177    pub actions_cache: bool,
178}
179
180impl BuildCache {
181    /// Doctor's word on where jobs' GitHub Actions cache is: for information, it fails nothing.
182    pub fn actions_cache_check(&self) -> api::Check {
183        api::Check {
184            name: api::ACTIONS_CACHE.to_owned(),
185            status: api::CheckStatus::Info,
186            detail: if self.enabled {
187                api::actions_cache_place(self.actions_cache)
188            } else {
189                "GitHub's, as the build cache is off"
190            }
191            .to_owned(),
192            fix: None,
193        }
194    }
195
196    fn validate(&self) -> Result<(), ConfigError> {
197        if self.enabled && self.max_size.0 == 0 {
198            return Err(ConfigError::ZeroLimit {
199                field: "caches.build.max_size".to_owned(),
200            });
201        }
202        Ok(())
203    }
204}
205
206impl Default for BuildCache {
207    fn default() -> BuildCache {
208        BuildCache {
209            enabled: true,
210            max_size: ByteSize(10 << 30),
211            branches: Vec::new(),
212            directory: None,
213            actions_cache: true,
214        }
215    }
216}
217
218fn default_reseed_at() -> ByteSize {
219    ByteSize(2 << 30)
220}
221
222/// Above the 8.7G a profile running Flutter and Android builds needs: its Flutter SDK, NDK and Gradle caches.
223fn default_snapshot_max() -> ByteSize {
224    ByteSize(10 << 30)
225}
226
227fn default_mirror_max_size() -> ByteSize {
228    ByteSize(10 << 30)
229}
230
231/// The Docker Hub pull-through mirror shared by all runners.
232#[derive(Debug, Clone, Deserialize)]
233#[serde(deny_unknown_fields)]
234pub struct Mirror {
235    /// Whether runners pull through the mirror.
236    pub enabled: bool,
237    /// The mirror container's memory limit.
238    pub memory: ByteSize,
239    /// The mirror container's CPUs.
240    pub cpus: u32,
241    /// How long content not pulled again is kept.
242    #[serde(deserialize_with = "duration")]
243    pub ttl: Duration,
244    /// Its storage is emptied above this size when no job runs.
245    #[serde(default = "default_mirror_max_size")]
246    pub max_size: ByteSize,
247}
248
249/// A size limit on a shared cache.
250#[derive(Debug, Clone, Deserialize)]
251#[serde(deny_unknown_fields)]
252pub struct CacheLimit {
253    /// The cache is emptied above this size when no job runs.
254    pub max_size: ByteSize,
255}
256
257/// Where runnerd's release workflow publishes the runner images, one tag per stage and version, such as
258/// `ghcr.io/ayagmar/runnerd-runner:jvm-0.0.29`.
259pub const PUBLISHED_RUNNER: &str = "ghcr.io/ayagmar/runnerd-runner";
260
261/// A runner image with its limits; every job of the profile gets one container with these limits.
262#[derive(Debug, Clone, Deserialize)]
263#[serde(try_from = "ProfileToml")]
264pub struct Profile {
265    /// The image its runners run: `image_stage` or `image`, exactly one of them.
266    pub image: Image,
267    /// CPUs per slot.
268    pub cpus: u32,
269    /// Memory per slot; the container's hard limit.
270    pub memory: ByteSize,
271    /// The most jobs of this profile running at once.
272    pub slots: u32,
273    /// Whether a failed job's runner is kept as a record, to open a shell in (`runnerctl shell`).
274    pub record_failures: bool,
275    /// The least `memory` auto sizing lowers it to; `None` for no bound.
276    pub min_memory: Option<ByteSize>,
277    /// The most `memory` auto sizing raises it to; `None` for what the slice's `MemoryMax` holds.
278    pub max_memory: Option<ByteSize>,
279    /// The most `slots` auto sizing raises them to; `None` for as many as the slice's `MemoryMax` holds.
280    pub max_slots: Option<u32>,
281    /// The GitHub-hosted runner its jobs are priced at (ADR 0072); `None` for the daemon's architecture's 2-core
282    /// standard runner.
283    pub priced_as: Option<api::Sku>,
284}
285
286/// The image a profile's runners run.
287#[derive(Debug, Clone, PartialEq, Eq)]
288pub enum Image {
289    /// `image_stage`: a stage of `image/Dockerfile` built on this host as `runnerd-runner:<stage>`.
290    Stage(String),
291    /// `image`: a reference runnerd pulls when Docker lacks it, such as
292    /// `ghcr.io/ayagmar/runnerd-runner:jvm-0.0.29`.
293    Pulled(String),
294}
295
296impl Image {
297    /// The reference Docker runs.
298    pub fn reference(&self) -> String {
299        match self {
300            Image::Stage(stage) => format!("runnerd-runner:{stage}"),
301            Image::Pulled(reference) => reference.clone(),
302        }
303    }
304}
305
306/// `[profiles.<name>]` as written, before `Profile` checks it names one image.
307#[derive(Deserialize)]
308#[serde(deny_unknown_fields)]
309struct ProfileToml {
310    image_stage: Option<String>,
311    image: Option<String>,
312    cpus: u32,
313    memory: ByteSize,
314    slots: u32,
315    #[serde(default)]
316    record_failures: bool,
317    min_memory: Option<ByteSize>,
318    max_memory: Option<ByteSize>,
319    max_slots: Option<u32>,
320    priced_as: Option<api::Sku>,
321}
322
323impl TryFrom<ProfileToml> for Profile {
324    type Error = String;
325
326    fn try_from(profile: ProfileToml) -> Result<Profile, String> {
327        let image = match (profile.image_stage, profile.image) {
328            (Some(stage), None) if !stage.is_empty() => Image::Stage(stage),
329            (None, Some(reference)) if !reference.is_empty() => Image::Pulled(reference),
330            (Some(_), Some(_)) => {
331                return Err(
332                    "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull), \
333                     not both"
334                        .to_owned(),
335                );
336            }
337            _ => {
338                return Err(
339                    "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull)"
340                        .to_owned(),
341                );
342            }
343        };
344        Ok(Profile {
345            image,
346            cpus: profile.cpus,
347            memory: profile.memory,
348            slots: profile.slots,
349            record_failures: profile.record_failures,
350            min_memory: profile.min_memory,
351            max_memory: profile.max_memory,
352            max_slots: profile.max_slots,
353            priced_as: profile.priced_as,
354        })
355    }
356}
357
358/// Whether a job asking for `wanted` may run on runners registered with `labels`: every label it asks for is one of
359/// theirs. GitHub compares labels without regard to case. A job asking for no label, as one that names only a runner
360/// group, matches no runners: nothing says which pool it is for (ADR 0058).
361pub fn labels_match(wanted: &[String], labels: &[String]) -> bool {
362    !wanted.is_empty()
363        && wanted.iter().all(|wanted| {
364            labels
365                .iter()
366                .any(|label| label.eq_ignore_ascii_case(wanted))
367        })
368}
369
370/// A repository to serve: `[[repositories]]`. Each entry is one of the repository's pools; entries with the same
371/// `name` serve it in several profiles, each with its own labels, which no two of them share (ADR 0058).
372#[derive(Debug, Deserialize)]
373#[serde(deny_unknown_fields)]
374pub struct Repository {
375    /// `owner/name` on GitHub.
376    pub name: String,
377    /// The profile the jobs this pool's labels match run in.
378    pub profile: String,
379    /// The labels its runners register with; a job runs on them when every label it asks for is one of these.
380    pub labels: Vec<String>,
381    /// How many idle runners it keeps registered for jobs not queued yet while it is active (ADR 0050); 0 turns warm
382    /// runners off. Default 1.
383    #[serde(default = "default_warm")]
384    pub warm: u32,
385    /// Whether its runners get the shared build cache, when `[caches.build]` is enabled. Default true.
386    #[serde(default = "default_true")]
387    pub build_cache: bool,
388    /// Whose runs may write to the shared build cache besides the branches' (ADR 0053), the same in every pool of
389    /// the repository. Default `"default-branch"`.
390    #[serde(default)]
391    pub build_cache_writers: BuildCacheWriters,
392}
393
394/// `build_cache_writers` of a `[[repositories]]`: whose jobs' uploads the shared build cache keeps (ADR 0053).
395#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
396#[serde(rename_all = "kebab-case")]
397pub enum BuildCacheWriters {
398    /// Pushes, scheduled and manually started runs of the default branch and of `[caches.build] branches`.
399    #[default]
400    DefaultBranch,
401    /// Those, and `pull_request` runs of the repository's own branches that people, not bots, opened and pushed.
402    PullRequests,
403}
404
405fn default_true() -> bool {
406    true
407}
408
409fn default_warm() -> u32 {
410    1
411}
412
413/// `[history]`: the job history database and how long it keeps what. Every field has a default.
414#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
415#[serde(deny_unknown_fields, default)]
416pub struct History {
417    /// The SQLite database; default `$XDG_STATE_HOME/runnerd/runnerd.db`.
418    pub database: Option<PathBuf>,
419    /// How long CPU and memory samples are kept; a job's totals and peaks stay. Default 7 days.
420    #[serde(deserialize_with = "duration")]
421    pub samples: Duration,
422    /// How long a finished job's log is kept. Default 30 days.
423    #[serde(deserialize_with = "duration")]
424    pub logs: Duration,
425    /// How long events are kept for clients resuming with `Last-Event-ID`. Default 30 days.
426    #[serde(deserialize_with = "duration")]
427    pub events: Duration,
428    /// How long a finished job's record is kept, with its steps, totals and runner. Default 90 days.
429    #[serde(deserialize_with = "duration")]
430    pub jobs: Duration,
431    /// The most log stored per job; later lines are dropped. Default 50M.
432    pub log_max: ByteSize,
433}
434
435impl Default for History {
436    fn default() -> History {
437        History {
438            database: None,
439            samples: Duration::from_hours(7 * 24),
440            logs: Duration::from_hours(30 * 24),
441            events: Duration::from_hours(30 * 24),
442            jobs: Duration::from_hours(90 * 24),
443            log_max: ByteSize(50 << 20),
444        }
445    }
446}
447
448/// `[records]`: how much the records of failed jobs may keep (`record_failures` under a profile turns them on).
449/// When a new record would pass a limit, the oldest records go first. Every field has a default.
450#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
451#[serde(deny_unknown_fields, default)]
452pub struct Records {
453    /// The most disk all records may take together. Default 20G.
454    pub max_total: ByteSize,
455    /// The most records kept. Default 10.
456    pub max_count: u32,
457    /// How long a record is kept. Default 7 days.
458    #[serde(deserialize_with = "duration")]
459    pub max_age: Duration,
460    /// The disk Docker must keep free: no record is made that would leave less. Default 10G.
461    pub min_free: ByteSize,
462}
463
464impl Default for Records {
465    fn default() -> Records {
466        Records {
467            max_total: ByteSize(20 << 30),
468            max_count: 10,
469            max_age: Duration::from_hours(7 * 24),
470            min_free: ByteSize(10 << 30),
471        }
472    }
473}
474
475/// `[sizing]`: how `runnerctl sizing` turns the job history's peaks into advice, and whether the daemon applies it
476/// itself. Every field has a default.
477#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
478#[serde(deny_unknown_fields, default)]
479pub struct Sizing {
480    /// The percent added to the largest sampled memory peak: a container is sampled every 2 s, so a spike between
481    /// two samples goes unseen. Default 15. Advice adds at least `api::PAGE_CACHE_ROOM`.
482    pub margin: u32,
483    /// The fewest jobs a profile, or a job of a repository's workflow, needs before its peaks become advice.
484    /// Default 10.
485    pub min_jobs: u32,
486    /// Whether the daemon writes each profile's advice into the config itself, within each profile's
487    /// `min_memory`, `max_memory` and `max_slots`. Default off.
488    pub auto: bool,
489    /// The least time between a change auto sizing makes to one profile and one that lowers anything in it. Default
490    /// 1 hour: a lowering that went too far is taken back at its first kill (docs/adr/0071).
491    #[serde(deserialize_with = "duration")]
492    pub interval: Duration,
493    /// The least time between a change auto sizing makes to one profile and one that only raises its `memory`,
494    /// `slots` or `cpus`; at most `interval`, which a longer one counts as. Default 1 hour.
495    #[serde(deserialize_with = "duration")]
496    pub raise_interval: Duration,
497    /// The most a change lowers a profile's `memory` or moves its `cpus`, in percent of them; a raise of `memory`,
498    /// which only a run that met the cap gives, goes straight to the advice. Default 50.
499    pub max_step: u32,
500}
501
502impl Sizing {
503    /// The least time between a change to one profile and one that only raises it: `raise_interval`, at most
504    /// `interval`.
505    pub fn raise_after(&self) -> Duration {
506        self.raise_interval.min(self.interval)
507    }
508}
509
510impl Default for Sizing {
511    fn default() -> Sizing {
512        Sizing {
513            margin: 15,
514            min_jobs: 10,
515            auto: false,
516            interval: Duration::from_hours(1),
517            raise_interval: Duration::from_hours(1),
518            max_step: 50,
519        }
520    }
521}
522
523/// `[web]`: the dashboard behind a reverse proxy, such as `tailscale serve`. Read when the daemon starts.
524#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
525#[serde(deny_unknown_fields, default)]
526pub struct Web {
527    /// The origins, besides the dashboard's own loopback address, whose pages may write: pause, reseed, save the
528    /// config. Default none.
529    pub origins: Vec<Origin>,
530    /// The one of `origins` that `runnerctl web` opens the dashboard on, such as a tailnet's address. Default none:
531    /// it opens the loopback address.
532    pub open: Option<Origin>,
533}
534
535impl Web {
536    /// Refuses an `open` that is not one of `origins`.
537    fn validate(&self) -> Result<(), ConfigError> {
538        match &self.open {
539            Some(open) if !self.origins.contains(open) => {
540                Err(ConfigError::OpenNotListed { open: open.clone() })
541            }
542            _ => Ok(()),
543        }
544    }
545}
546
547/// `[notify]`: where the daemon sends notifications (ADR 0051). Without targets nothing leaves the machine.
548#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
549#[serde(deny_unknown_fields, default)]
550pub struct Notify {
551    /// When the digest goes to the targets that want it, in the host's local time. Default `09:00`.
552    pub digest_at: DayTime,
553    /// The targets, each `[[notify.targets]]`.
554    pub targets: Vec<NotifyTarget>,
555}
556
557impl Default for Notify {
558    fn default() -> Notify {
559        Notify {
560            digest_at: DayTime { minutes: 9 * 60 },
561            targets: Vec::new(),
562        }
563    }
564}
565
566impl Notify {
567    /// Refuses a target without a name, with a name another has, with a URL that is not `http` or `https`, or with
568    /// a header that cannot be sent. Says which target and which field, never the URL or a header's value.
569    fn validate(&self) -> Result<(), ConfigError> {
570        let refuse = |target: &str, why: &'static str| {
571            Err(ConfigError::Notify {
572                target: target.to_owned(),
573                why,
574            })
575        };
576        for (index, target) in self.targets.iter().enumerate() {
577            let name = target.name.as_str();
578            if name.trim().is_empty() || name.chars().any(char::is_control) {
579                return refuse(name, "name is empty");
580            }
581            if self.targets[..index].iter().any(|other| other.name == name) {
582                return refuse(name, "another target has this name");
583            }
584            let url = target.url.expose();
585            if url == SECRET_SHOWN {
586                return refuse(
587                    name,
588                    "url is <secret>, which runnerd fills in only for a target of the same name that had one; write the url",
589                );
590            }
591            let Some(rest) = url
592                .strip_prefix("https://")
593                .or_else(|| url.strip_prefix("http://"))
594            else {
595                return refuse(name, "url does not start with https:// or http://");
596            };
597            let (host, path) = rest.split_once('/').unwrap_or((rest, ""));
598            if host.is_empty() || url.chars().any(|c| c.is_whitespace() || c.is_control()) {
599                return refuse(name, "url is not a URL");
600            }
601            if target.kind == NotifyKind::Ntfy
602                && (path.is_empty() || path.contains(['/', '?', '#']))
603            {
604                return refuse(
605                    name,
606                    "url is not an ntfy topic's, such as https://ntfy.sh/<topic>",
607                );
608            }
609            for (header, value) in &target.headers {
610                if value.expose() == SECRET_SHOWN {
611                    return refuse(
612                        name,
613                        "a header is <secret>, which runnerd fills in only for a target of the same name that had it; write its value",
614                    );
615                }
616                let token = |c: char| c.is_ascii_alphanumeric() || "!#$%&'*+-.^_`|~".contains(c);
617                if header.is_empty() || !header.chars().all(token) {
618                    return refuse(name, "a header's name is not a header name");
619                }
620                if value.expose().chars().any(char::is_control) {
621                    return refuse(name, "a header's value has a control character");
622                }
623            }
624            if target.events.contains(&api::NotifyEvent::Test) {
625                return refuse(
626                    name,
627                    "events lists test, which only runnerctl notify test sends",
628                );
629            }
630        }
631        Ok(())
632    }
633}
634
635/// `[[notify.targets]]`: one place notifications go.
636#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
637#[serde(deny_unknown_fields)]
638pub struct NotifyTarget {
639    /// What logs and `runnerctl notify test` call it, such as `phone`; the history keeps where it got to by it.
640    pub name: String,
641    /// What it takes.
642    pub kind: NotifyKind,
643    /// Where it is posted: the webhook's URL, Discord's included, or the ntfy topic's, such as `https://ntfy.sh/<topic>`.
644    /// A secret.
645    pub url: Secret,
646    /// Headers sent with each post, such as an ntfy access token's `Authorization`. Their values are secrets.
647    #[serde(default)]
648    pub headers: BTreeMap<String, Secret>,
649    /// What it is sent. Default `failed`, `lost`, `runner` and `insights`.
650    #[serde(default = "default_notify_events")]
651    pub events: Vec<api::NotifyEvent>,
652}
653
654fn default_notify_events() -> Vec<api::NotifyEvent> {
655    vec![
656        api::NotifyEvent::Failed,
657        api::NotifyEvent::Lost,
658        api::NotifyEvent::Runner,
659        api::NotifyEvent::Insights,
660    ]
661}
662
663/// What a notification target takes.
664#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
665#[serde(rename_all = "kebab-case")]
666pub enum NotifyKind {
667    /// `api::Notification` as JSON.
668    Webhook,
669    /// An ntfy topic: the title, message, priority and link, published as ntfy's JSON.
670    Ntfy,
671    /// A Discord channel's webhook: the title, message and link as one embed, coloured by event, that pings no one.
672    Discord,
673}
674
675/// A value no `Debug` output, log line or error shows: a notification target's URL or header.
676#[derive(Clone, PartialEq, Eq, Deserialize)]
677#[serde(transparent)]
678pub struct Secret(String);
679
680impl Secret {
681    /// The value, for the request that sends it.
682    pub fn expose(&self) -> &str {
683        &self.0
684    }
685}
686
687impl From<&str> for Secret {
688    fn from(value: &str) -> Secret {
689        Secret(value.to_owned())
690    }
691}
692
693impl fmt::Debug for Secret {
694    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
695        f.write_str("<secret>")
696    }
697}
698
699/// A time of day, `HH:MM`, 24-hour.
700#[derive(Debug, Clone, Copy, PartialEq, Eq)]
701pub struct DayTime {
702    /// Minutes after midnight.
703    pub minutes: u32,
704}
705
706impl FromStr for DayTime {
707    type Err = String;
708
709    fn from_str(text: &str) -> Result<DayTime, String> {
710        let invalid = || format!("invalid time of day {text:?}, expected HH:MM such as \"09:00\"");
711        let (hour, minute) = text.split_once(':').ok_or_else(invalid)?;
712        let digits = |part: &str| part.len() == 2 && part.chars().all(|c| c.is_ascii_digit());
713        if !digits(hour) || !digits(minute) {
714            return Err(invalid());
715        }
716        let (hour, minute): (u32, u32) = (
717            hour.parse().map_err(|_| invalid())?,
718            minute.parse().map_err(|_| invalid())?,
719        );
720        if hour > 23 || minute > 59 {
721            return Err(invalid());
722        }
723        Ok(DayTime {
724            minutes: hour * 60 + minute,
725        })
726    }
727}
728
729impl fmt::Display for DayTime {
730    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
731        write!(f, "{:02}:{:02}", self.minutes / 60, self.minutes % 60)
732    }
733}
734
735impl<'de> Deserialize<'de> for DayTime {
736    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
737        String::deserialize(deserializer)?
738            .parse()
739            .map_err(de::Error::custom)
740    }
741}
742
743/// An origin as a browser sends it in `Origin`: `https://host[:port]`, or `http://` for a loopback host, in lower
744/// case, without the scheme's default port, and an address written as the URL standard serializes it. Nothing after
745/// the port: no path, query or user.
746#[derive(Debug, Clone, PartialEq, Eq)]
747pub struct Origin(String);
748
749impl Origin {
750    /// The origin as a browser sends it, such as `https://host.example.ts.net:8443`.
751    pub fn as_str(&self) -> &str {
752        &self.0
753    }
754}
755
756impl FromStr for Origin {
757    type Err = String;
758
759    fn from_str(s: &str) -> Result<Origin, String> {
760        let invalid = |why: &str| {
761            format!(
762                "invalid origin {s:?}: {why}; write the scheme, host and port only, such as \
763                 \"https://host.example.ts.net:8443\""
764            )
765        };
766        let (scheme, rest) = s
767            .split_once("://")
768            .ok_or_else(|| invalid("it has no scheme"))?;
769        let scheme = scheme.to_ascii_lowercase();
770        if rest.contains(['/', '?', '#']) {
771            return Err(invalid("it has a path"));
772        }
773        if rest.contains('@') {
774            return Err(invalid("it names a user"));
775        }
776        let (host, port) = match rest.strip_prefix('[') {
777            Some(v6) => {
778                let (address, after) = v6
779                    .split_once(']')
780                    .ok_or_else(|| invalid("its IPv6 address has no closing ]"))?;
781                let port = match after {
782                    "" => None,
783                    _ => Some(after.strip_prefix(':').ok_or_else(|| invalid("its port"))?),
784                };
785                (format!("[{address}]"), port)
786            }
787            None => match rest.split_once(':') {
788                Some((host, port)) => (host.to_owned(), Some(port)),
789                None => (rest.to_owned(), None),
790            },
791        };
792        let host = host.to_ascii_lowercase();
793        let not_a_host = || invalid("its host is not a name or an address");
794        // An address is kept as a browser serializes it, or it would never match the `Origin` the browser sends.
795        let (host, loopback) =
796            if let Some(v6) = host.strip_prefix('[').and_then(|v6| v6.strip_suffix(']')) {
797                let address: Ipv6Addr = v6.parse().map_err(|_| not_a_host())?;
798                (format!("[{}]", ipv6_origin(address)), address.is_loopback())
799            } else {
800                if host.is_empty()
801                    || !host
802                        .chars()
803                        .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.')
804                {
805                    return Err(not_a_host());
806                }
807                // As the URL standard reads a host: one whose last label is a number is an IPv4 address.
808                let name = host.strip_suffix('.').unwrap_or(&host);
809                let last = name.rsplit('.').next().unwrap_or_default();
810                let numeric = !last.is_empty()
811                    && (last.bytes().all(|byte| byte.is_ascii_digit())
812                        || last
813                            .strip_prefix("0x")
814                            .is_some_and(|hex| hex.bytes().all(|byte| byte.is_ascii_hexdigit())));
815                if numeric {
816                    let address: Ipv4Addr = name.parse().map_err(|_| {
817                        invalid("its IPv4 address is not four decimal numbers, such as 192.0.2.1")
818                    })?;
819                    (address.to_string(), address.is_loopback())
820                } else {
821                    let loopback = host == "localhost";
822                    (host, loopback)
823                }
824            };
825        let port = port
826            .map(|port| {
827                Some(port)
828                    .filter(|port| port.bytes().all(|byte| byte.is_ascii_digit()))
829                    .and_then(|port| port.parse::<u16>().ok())
830                    .filter(|port| *port != 0)
831                    .ok_or_else(|| invalid("its port is not a number from 1 to 65535"))
832            })
833            .transpose()?;
834        let default_port = match scheme.as_str() {
835            "https" => 443,
836            "http" if loopback => 80,
837            "http" => {
838                return Err(invalid(
839                    "http is for a loopback host only, since the session cookie would cross the network in \
840                     the clear; use https",
841                ));
842            }
843            _ => return Err(invalid("its scheme is not https")),
844        };
845        Ok(Origin(match port {
846            Some(port) if port != default_port => format!("{scheme}://{host}:{port}"),
847            _ => format!("{scheme}://{host}"),
848        }))
849    }
850}
851
852/// An IPv6 address as the URL standard serializes it: lower-case hex pieces, the first longest run of two or more
853/// zero pieces as `::`, and no dotted IPv4 tail.
854fn ipv6_origin(address: Ipv6Addr) -> String {
855    let pieces = address.segments();
856    let (mut zeros_at, mut zeros) = (0, 0);
857    let mut at = 0;
858    while at < pieces.len() {
859        let run = pieces[at..].iter().take_while(|piece| **piece == 0).count();
860        if run > zeros {
861            (zeros_at, zeros) = (at, run);
862        }
863        at += run.max(1);
864    }
865    let hex = |pieces: &[u16]| {
866        pieces
867            .iter()
868            .map(|piece| format!("{piece:x}"))
869            .collect::<Vec<_>>()
870            .join(":")
871    };
872    if zeros < 2 {
873        return hex(&pieces);
874    }
875    format!(
876        "{}::{}",
877        hex(&pieces[..zeros_at]),
878        hex(&pieces[zeros_at + zeros..])
879    )
880}
881
882impl fmt::Display for Origin {
883    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
884        f.write_str(&self.0)
885    }
886}
887
888impl<'de> Deserialize<'de> for Origin {
889    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
890        String::deserialize(deserializer)?
891            .parse()
892            .map_err(de::Error::custom)
893    }
894}
895
896/// Why a config cannot be used.
897#[derive(Debug, thiserror::Error)]
898pub enum ConfigError {
899    /// The file cannot be read.
900    #[error("cannot read {}: {source}", path.display())]
901    Read {
902        /// The config file.
903        path: PathBuf,
904        /// Why reading it failed.
905        source: std::io::Error,
906    },
907    /// The file is not valid TOML or does not match the config's shape.
908    #[error(transparent)]
909    Parse(#[from] toml::de::Error),
910    /// A repository names a profile `[profiles]` does not define.
911    #[error(
912        "repository {repository} uses profile {profile:?}, which is not defined under [profiles]; {}",
913        defined_profiles(.defined)
914    )]
915    UnknownProfile {
916        /// The repository's `owner/name`.
917        repository: String,
918        /// The profile it names.
919        profile: String,
920        /// The profiles `[profiles]` defines.
921        defined: Vec<String>,
922    },
923    /// A repository has no labels, or an empty one.
924    #[error("repository {repository} needs at least one non-empty label")]
925    MissingLabels {
926        /// The repository's `owner/name`.
927        repository: String,
928    },
929    /// Two `[[repositories]]` entries of one repository use the same profile.
930    #[error(
931        "repository {repository} has two entries with profile {profile:?}; give each of its pools its own profile"
932    )]
933    PoolProfileTwice {
934        /// The repository's `owner/name`.
935        repository: String,
936        /// The profile both entries name.
937        profile: String,
938    },
939    /// Two `[[repositories]]` entries of one repository share a label, so a job asking only for labels they share
940    /// could run in either.
941    #[error(
942        "repository {repository}'s pools in {first} and {second} both have the label {label:?}, so a job asking for \
943         it could land in either; give each pool labels of its own, such as only {second:?}'s own label for the \
944         lighter one"
945    )]
946    PoolLabelsOverlap {
947        /// The repository's `owner/name`.
948        repository: String,
949        /// The profile of the first entry.
950        first: String,
951        /// The profile of the later entry.
952        second: String,
953        /// A label both have.
954        label: String,
955    },
956    /// Two `[[repositories]]` entries of one repository say differently whose runs write to the build cache.
957    #[error(
958        "repository {repository}'s entries set build_cache_writers differently; whose runs may write to the build \
959         cache is the repository's, so set the same in each"
960    )]
961    PoolWritersDiffer {
962        /// The repository's `owner/name`.
963        repository: String,
964    },
965    /// `unsafe-dind` without `allow_unsafe = true`.
966    #[error(
967        "isolation \"unsafe-dind\" runs runner containers privileged, so a job can take over the host; \
968         set allow_unsafe = true under [host] to use it anyway"
969    )]
970    UnsafeNotAllowed,
971    /// A limit is 0.
972    #[error("{field} is 0; set it above 0")]
973    ZeroLimit {
974        /// The setting, such as `profiles.app.cpus`.
975        field: String,
976    },
977    /// `cpu_overcommit` is not a number above 0 and at most `MAX_CPU_OVERCOMMIT`.
978    #[error(
979        "host.cpu_overcommit is {value}; set it above 0 and at most {MAX_CPU_OVERCOMMIT}, such as 1.0"
980    )]
981    CpuOvercommit {
982        /// The value set.
983        value: f64,
984    },
985    /// `[web] open` is not one of `[web] origins`.
986    #[error(
987        "web.open is {open}, which is not one of [web] origins; add it to origins, or remove open"
988    )]
989    OpenNotListed {
990        /// The origin `open` names.
991        open: Origin,
992    },
993    /// A profile's `memory` or `slots` are outside its `min_memory`, `max_memory` or `max_slots`, or its
994    /// `min_memory` is above its `max_memory`.
995    #[error("profiles.{profile}: {why}")]
996    SizingBounds {
997        /// The profile.
998        profile: String,
999        /// Which bound, such as `memory is above its max_memory`.
1000        why: &'static str,
1001    },
1002    /// A `[[notify.targets]]` entry cannot be used. Never says its URL or a header's value.
1003    #[error("notify target {target:?}: {why}")]
1004    Notify {
1005        /// The target's name.
1006        target: String,
1007        /// What is wrong, such as `url does not start with https:// or http://`.
1008        why: &'static str,
1009    },
1010    /// `[host] memory_ceiling` is under one runner of a profile and the mirror, so that profile could never start.
1011    #[error(
1012        "host.memory_ceiling is {ceiling}, under the {needs} one runner of {profile} and the mirror take; set it \
1013         to the most memory the host can grow to, such as the VM's maximum"
1014    )]
1015    CeilingUnderProfile {
1016        /// The profile.
1017        profile: String,
1018        /// Its `memory` and the mirror's.
1019        needs: ByteSize,
1020        /// The ceiling set.
1021        ceiling: ByteSize,
1022    },
1023    /// The caps add up past `u64::MAX` bytes.
1024    #[error("slots × memory over all profiles plus the mirror is too large to add up")]
1025    BudgetOverflow,
1026    /// `admission = "measured"` with a slice that has no `MemoryMax` to admit against.
1027    #[error(
1028        "admission = \"measured\" admits runners against the slice {slice}'s MemoryMax, and it has none; install \
1029         a unit file for it (`runnerd doctor` prints one)"
1030    )]
1031    MeasuredWithoutMemoryMax {
1032        /// The slice runners run in.
1033        slice: String,
1034    },
1035    /// Under `admission = "measured"`, one runner of a profile and the mirror do not fit `MemoryMax`.
1036    #[error(
1037        "one runner of {profile} and the mirror take {needs}, the slice {slice}'s MemoryMax is {memory_max}; lower \
1038         its `memory` or raise the slice's MemoryMax"
1039    )]
1040    ProfileOverBudget {
1041        /// The profile.
1042        profile: String,
1043        /// Its `memory` and the mirror's.
1044        needs: ByteSize,
1045        /// The slice runners run in.
1046        slice: String,
1047        /// The slice's `MemoryMax`.
1048        memory_max: ByteSize,
1049    },
1050    /// The caps add up to more than the slice's `MemoryMax`.
1051    #[error(
1052        "slots × memory over all profiles plus the mirror is {total}, \
1053         the slice {slice}'s MemoryMax is {memory_max}; lower `slots` or `memory`, or raise the slice's \
1054         MemoryMax (`runnerd doctor` prints its unit)"
1055    )]
1056    OverBudget {
1057        /// What every runner and the mirror may use at once.
1058        total: ByteSize,
1059        /// The slice runners run in.
1060        slice: String,
1061        /// The slice's `MemoryMax`.
1062        memory_max: ByteSize,
1063    },
1064}
1065
1066fn defined_profiles(defined: &[String]) -> String {
1067    if defined.is_empty() {
1068        "it defines none".to_owned()
1069    } else {
1070        format!("defined: {}", defined.join(", "))
1071    }
1072}
1073
1074/// What a valid config should still fix.
1075#[derive(Debug, Clone, PartialEq, Eq)]
1076pub enum Warning {
1077    /// The slice has no unit file, so systemd creates it without limits.
1078    UnlimitedSlice {
1079        /// The slice runners run in.
1080        slice: String,
1081        /// What every runner and the mirror may use at once.
1082        total: ByteSize,
1083    },
1084}
1085
1086impl fmt::Display for Warning {
1087    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1088        match self {
1089            Warning::UnlimitedSlice { slice, total } => write!(
1090                f,
1091                "the slice {slice} has no MemoryMax, so nothing holds runners to their {total} of caps; \
1092                 install a unit file for it"
1093            ),
1094        }
1095    }
1096}
1097
1098impl Config {
1099    /// Reads and parses a config file.
1100    pub fn load(path: &Path) -> Result<Config, ConfigError> {
1101        let text = std::fs::read_to_string(path).map_err(|source| ConfigError::Read {
1102            path: path.to_owned(),
1103            source,
1104        })?;
1105        Config::parse(&text)
1106    }
1107
1108    /// Parses a config from TOML text.
1109    pub fn parse(text: &str) -> Result<Config, ConfigError> {
1110        Ok(toml::from_str(text)?)
1111    }
1112
1113    /// Parses `text` and validates it against the `MemoryMax` that `memory_max` reads for the slice it names:
1114    /// what `runnerd check-config` prints and `POST /v1/config/check` answers. Returns the config if it is valid.
1115    pub fn check(
1116        text: &str,
1117        memory_max: impl FnOnce(&str) -> std::io::Result<Option<ByteSize>>,
1118    ) -> (Option<Config>, api::ConfigCheck) {
1119        let config = match Config::parse(text) {
1120            Ok(config) => config,
1121            Err(error) => return (None, budget::invalid(error.to_string(), None, None)),
1122        };
1123        let max = match memory_max(&config.host.slice) {
1124            Ok(max) => max,
1125            Err(error) => {
1126                return (
1127                    None,
1128                    budget::invalid(error.to_string(), Some(&config), None),
1129                );
1130            }
1131        };
1132        let check = config.check_against(max);
1133        if check.valid {
1134            (Some(config), check)
1135        } else {
1136            (None, check)
1137        }
1138    }
1139
1140    /// The mirror's memory cap, 0 when it is disabled.
1141    pub fn mirror_memory(&self) -> u64 {
1142        if self.caches.mirror.enabled {
1143            self.caches.mirror.memory.0
1144        } else {
1145            0
1146        }
1147    }
1148
1149    /// The memory every runner and the mirror may use at once.
1150    pub fn memory_caps(&self) -> Result<ByteSize, ConfigError> {
1151        let mirror = self.mirror_memory();
1152        self.profiles
1153            .values()
1154            .try_fold(mirror, |total, p| {
1155                p.memory
1156                    .0
1157                    .checked_mul(u64::from(p.slots))?
1158                    .checked_add(total)
1159            })
1160            .map(ByteSize)
1161            .ok_or(ConfigError::BudgetOverflow)
1162    }
1163
1164    /// The memory `[host] admission` must fit in `MemoryMax`: every cap at once under caps; under measured, the
1165    /// largest single runner with the mirror, since `slots` there only bounds how many run at once.
1166    pub fn memory_needed(&self) -> Result<ByteSize, ConfigError> {
1167        match self.host.admission {
1168            Admission::Caps => self.memory_caps(),
1169            Admission::Measured => {
1170                let largest = self
1171                    .profiles
1172                    .values()
1173                    .map(|p| p.memory.0)
1174                    .max()
1175                    .unwrap_or(0);
1176                largest
1177                    .checked_add(self.mirror_memory())
1178                    .map(ByteSize)
1179                    .ok_or(ConfigError::BudgetOverflow)
1180            }
1181        }
1182    }
1183
1184    /// The most `memory` one runner of any profile may have under `[host] memory_ceiling`: the ceiling less the
1185    /// mirror while it is enabled, as no host of that size starts more beside it. `None` without a ceiling. The
1186    /// config check refuses a profile above it, and auto sizing raises none past it (ADR 0086).
1187    pub fn runner_ceiling(&self) -> Option<u64> {
1188        self.host
1189            .memory_ceiling
1190            .map(|ceiling| ceiling.0.saturating_sub(self.mirror_memory()))
1191    }
1192
1193    /// Refuses a `[host] memory_ceiling` under one runner of the largest profile beside the mirror
1194    /// (`runner_ceiling`): no host of that size starts one, so it is a typo, such as `24M` for `24G`. Whether the
1195    /// host's `MemTotal` is already past it, and what the margin leaves, `runnerd doctor` says, as the config check
1196    /// does not read the host.
1197    fn check_memory_ceiling(&self) -> Result<(), ConfigError> {
1198        let (Some(ceiling), Some(most)) = (self.host.memory_ceiling, self.runner_ceiling()) else {
1199            return Ok(());
1200        };
1201        let largest = self
1202            .profiles
1203            .iter()
1204            .max_by_key(|(_, profile)| profile.memory.0);
1205        match largest {
1206            Some((profile, largest)) if largest.memory.0 > most => {
1207                Err(ConfigError::CeilingUnderProfile {
1208                    profile: profile.clone(),
1209                    needs: ByteSize(largest.memory.0.saturating_add(self.mirror_memory())),
1210                    ceiling,
1211                })
1212            }
1213            _ => Ok(()),
1214        }
1215    }
1216
1217    /// Refuses a profile outside the bounds auto sizing keeps it in, or whose bounds cross.
1218    fn check_sizing_bounds(&self) -> Result<(), ConfigError> {
1219        for (name, p) in &self.profiles {
1220            let memory = p.memory.0;
1221            let field = if p
1222                .min_memory
1223                .zip(p.max_memory)
1224                .is_some_and(|(min, max)| min.0 > max.0)
1225            {
1226                "min_memory is above its max_memory"
1227            } else if p.min_memory.is_some_and(|min| memory < min.0) {
1228                "memory is below its min_memory"
1229            } else if p.max_memory.is_some_and(|max| memory > max.0) {
1230                "memory is above its max_memory"
1231            } else if p.max_slots.is_some_and(|max| p.slots > max) {
1232                "slots are above its max_slots"
1233            } else {
1234                continue;
1235            };
1236            return Err(ConfigError::SizingBounds {
1237                profile: name.clone(),
1238                why: field,
1239            });
1240        }
1241        Ok(())
1242    }
1243
1244    /// Checks that each repository's pools tell its jobs apart: each in its own profile, with labels no other of its
1245    /// pools has, so at most one pool matches a job; and that they agree on whose runs write to the build cache.
1246    fn check_pools(&self) -> Result<(), ConfigError> {
1247        for (at, first) in self.repositories.iter().enumerate() {
1248            for second in self.repositories[at + 1..]
1249                .iter()
1250                .filter(|second| second.name == first.name)
1251            {
1252                if second.profile == first.profile {
1253                    return Err(ConfigError::PoolProfileTwice {
1254                        repository: first.name.clone(),
1255                        profile: first.profile.clone(),
1256                    });
1257                }
1258                if let Some(label) = first.labels.iter().find(|label| {
1259                    second
1260                        .labels
1261                        .iter()
1262                        .any(|other| other.eq_ignore_ascii_case(label))
1263                }) {
1264                    return Err(ConfigError::PoolLabelsOverlap {
1265                        repository: first.name.clone(),
1266                        first: first.profile.clone(),
1267                        second: second.profile.clone(),
1268                        label: label.clone(),
1269                    });
1270                }
1271                if second.build_cache_writers != first.build_cache_writers {
1272                    return Err(ConfigError::PoolWritersDiffer {
1273                        repository: first.name.clone(),
1274                    });
1275                }
1276            }
1277        }
1278        Ok(())
1279    }
1280
1281    /// The pool of `repository` whose labels match a job asking for `labels`: every label it asks for is one of
1282    /// the pool's. `None` when no pool of it does.
1283    pub fn pool(&self, repository: &str, labels: &[String]) -> Option<&Repository> {
1284        self.repositories
1285            .iter()
1286            .find(|pool| pool.name == repository && labels_match(labels, &pool.labels))
1287    }
1288
1289    /// The pool of `repository` in `profile`, whose runners start in that profile's slots.
1290    pub fn pool_in(&self, repository: &str, profile: &str) -> Option<&Repository> {
1291        self.repositories
1292            .iter()
1293            .find(|pool| pool.name == repository && pool.profile == profile)
1294    }
1295
1296    /// The GitHub-hosted runner each profile's jobs are priced at (ADR 0072).
1297    pub fn pricing(&self) -> api::Pricing {
1298        api::Pricing {
1299            profiles: self
1300                .profiles
1301                .iter()
1302                .filter_map(|(name, profile)| Some((name.clone(), profile.priced_as?)))
1303                .collect(),
1304            default_sku: api::Sku::standard_for(std::env::consts::ARCH),
1305        }
1306    }
1307
1308    /// The repositories served, each once, in the order the config first names them.
1309    pub fn repository_names(&self) -> Vec<String> {
1310        let mut names: Vec<String> = Vec::new();
1311        for repository in &self.repositories {
1312            if !names.contains(&repository.name) {
1313                names.push(repository.name.clone());
1314            }
1315        }
1316        names
1317    }
1318
1319    /// Checks the config against the slice's `MemoryMax` (`None` when the slice is unlimited).
1320    pub fn validate(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1321        if self.host.isolation == Isolation::UnsafeDind && !self.host.allow_unsafe {
1322            return Err(ConfigError::UnsafeNotAllowed);
1323        }
1324        let zero_limits = self.profiles.iter().flat_map(|(name, p)| {
1325            [
1326                (p.memory.0 == 0, format!("profiles.{name}.memory")),
1327                (p.cpus == 0, format!("profiles.{name}.cpus")),
1328                (p.slots == 0, format!("profiles.{name}.slots")),
1329                (p.max_slots == Some(0), format!("profiles.{name}.max_slots")),
1330            ]
1331        });
1332        let mirror = &self.caches.mirror;
1333        let mirror_zero_limits = [
1334            (
1335                mirror.enabled && mirror.memory.0 == 0,
1336                "caches.mirror.memory".to_owned(),
1337            ),
1338            (
1339                mirror.enabled && mirror.cpus == 0,
1340                "caches.mirror.cpus".to_owned(),
1341            ),
1342            (
1343                self.host.poll_interval.is_zero(),
1344                "host.poll_interval".to_owned(),
1345            ),
1346            (
1347                self.host.memory_ceiling == Some(ByteSize(0)),
1348                "host.memory_ceiling".to_owned(),
1349            ),
1350            (self.records.max_count == 0, "records.max_count".to_owned()),
1351            (self.sizing.min_jobs == 0, "sizing.min_jobs".to_owned()),
1352            (self.sizing.interval.is_zero(), "sizing.interval".to_owned()),
1353            (
1354                self.sizing.raise_interval.is_zero(),
1355                "sizing.raise_interval".to_owned(),
1356            ),
1357            (self.sizing.max_step == 0, "sizing.max_step".to_owned()),
1358            (
1359                self.records.max_total.0 == 0,
1360                "records.max_total".to_owned(),
1361            ),
1362        ];
1363        if let Some((_, field)) = zero_limits
1364            .chain(mirror_zero_limits)
1365            .find(|(zero, _)| *zero)
1366        {
1367            return Err(ConfigError::ZeroLimit { field });
1368        }
1369        self.check_sizing_bounds()?;
1370        self.caches.build.validate()?;
1371        self.host.check_cpu_overcommit()?;
1372        self.check_memory_ceiling()?;
1373        self.web.validate()?;
1374        self.notify.validate()?;
1375        for repository in &self.repositories {
1376            if !self.profiles.contains_key(&repository.profile) {
1377                return Err(ConfigError::UnknownProfile {
1378                    repository: repository.name.clone(),
1379                    profile: repository.profile.clone(),
1380                    defined: self.profiles.keys().cloned().collect(),
1381                });
1382            }
1383            if repository.labels.is_empty() || repository.labels.iter().any(|l| l.trim().is_empty())
1384            {
1385                return Err(ConfigError::MissingLabels {
1386                    repository: repository.name.clone(),
1387                });
1388            }
1389        }
1390        self.check_pools()?;
1391        self.check_budget(memory_max)
1392    }
1393
1394    /// Holds what `[host] admission` needs to the slice's `MemoryMax`; warns when the slice has none to hold caps to.
1395    fn check_budget(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1396        let total = self.memory_caps()?;
1397        if self.host.admission == Admission::Measured {
1398            let Some(memory_max) = memory_max else {
1399                return Err(ConfigError::MeasuredWithoutMemoryMax {
1400                    slice: self.host.slice.clone(),
1401                });
1402            };
1403            let mirror = self.mirror_memory();
1404            if let Some((profile, needs)) = self
1405                .profiles
1406                .iter()
1407                .map(|(name, profile)| (name, profile.memory.0.saturating_add(mirror)))
1408                .find(|(_, needs)| *needs > memory_max.0)
1409            {
1410                return Err(ConfigError::ProfileOverBudget {
1411                    profile: profile.clone(),
1412                    needs: ByteSize(needs),
1413                    slice: self.host.slice.clone(),
1414                    memory_max,
1415                });
1416            }
1417            // Without a profile, the mirror alone must fit: the caps sum, which is all `memory_needed` holds.
1418            let needed = self.memory_needed()?;
1419            if needed > memory_max {
1420                return Err(ConfigError::OverBudget {
1421                    total: needed,
1422                    slice: self.host.slice.clone(),
1423                    memory_max,
1424                });
1425            }
1426            return Ok(Vec::new());
1427        }
1428        match memory_max {
1429            Some(memory_max) if total > memory_max => Err(ConfigError::OverBudget {
1430                total,
1431                slice: self.host.slice.clone(),
1432                memory_max,
1433            }),
1434            Some(_) => Ok(Vec::new()),
1435            None => Ok(vec![Warning::UnlimitedSlice {
1436                slice: self.host.slice.clone(),
1437                total,
1438            }]),
1439        }
1440    }
1441}
1442
1443/// Why `resize_profile` cannot change a config.
1444#[derive(Debug, thiserror::Error)]
1445pub enum ResizeError {
1446    /// The text is not valid TOML.
1447    #[error(transparent)]
1448    Parse(#[from] toml_edit::TomlError),
1449    /// `[profiles]` does not define the profile.
1450    #[error("the config has no profile {0:?} under [profiles]")]
1451    NoProfile(String),
1452}
1453
1454/// `text` with the profile's `memory` and `slots` replaced, and its `cpus` when `resize` has them, everything else as
1455/// written, comments included: what applying a sizing suggestion sends to `PUT /v1/config`, which validates it.
1456pub fn resize_profile(
1457    text: &str,
1458    profile: &str,
1459    resize: api::Resize,
1460) -> Result<String, ResizeError> {
1461    let mut document: toml_edit::DocumentMut = text.parse()?;
1462    let table = document
1463        .get_mut("profiles")
1464        .and_then(|profiles| profiles.get_mut(profile))
1465        .and_then(toml_edit::Item::as_table_like_mut)
1466        .ok_or_else(|| ResizeError::NoProfile(profile.to_owned()))?;
1467    let values = [
1468        (
1469            "memory",
1470            toml_edit::Value::from(ByteSize(resize.memory).exact()),
1471        ),
1472        ("slots", toml_edit::Value::from(i64::from(resize.slots))),
1473    ];
1474    let cpus = resize
1475        .cpus
1476        .map(|cpus| ("cpus", toml_edit::Value::from(i64::from(cpus))));
1477    for (key, mut value) in values.into_iter().chain(cpus) {
1478        // The comment after the old value stays after the new one.
1479        if let Some(old) = table.get(key).and_then(toml_edit::Item::as_value) {
1480            *value.decor_mut() = old.decor().clone();
1481        }
1482        table.insert(key, toml_edit::Item::Value(value));
1483    }
1484    Ok(document.to_string())
1485}
1486
1487/// `profile`'s `memory`, `slots` and `cpus` in the config `text`, as a sizing start changes them from; `None` when
1488/// `text` is not a config or has no such profile.
1489pub fn profile_sizes(text: &str, profile: &str) -> Option<api::Resize> {
1490    let config = Config::parse(text).ok()?;
1491    let sizes = config.profiles.get(profile)?;
1492    Some(api::Resize {
1493        memory: sizes.memory.0,
1494        slots: sizes.slots,
1495        cpus: Some(sizes.cpus),
1496    })
1497}
1498
1499/// Where `resize_profile` wrote `resize` in `text`: the bytes from the first of the profile's `memory`, `slots` and,
1500/// when `resize` has them, `cpus` settings to the end of the last, so a client can select them. `None` when `text` is
1501/// not TOML or the profile sets none of them.
1502pub fn resized_at(
1503    text: &str,
1504    profile: &str,
1505    resize: api::Resize,
1506) -> Option<std::ops::Range<usize>> {
1507    let document = toml_edit::Document::parse(text).ok()?;
1508    let table = document
1509        .as_table()
1510        .get("profiles")?
1511        .get(profile)?
1512        .as_table_like()?;
1513    let cpus = resize.cpus.map(|_| "cpus");
1514    ["memory", "slots"]
1515        .into_iter()
1516        .chain(cpus)
1517        .filter_map(|key| {
1518            let (key, item) = table.get_key_value(key)?;
1519            Some(key.span()?.start..item.span()?.end)
1520        })
1521        .reduce(|first, next| first.start.min(next.start)..first.end.max(next.end))
1522}
1523
1524/// What `GET /v1/config` shows in place of a notification target's URL and header values (ADR 0051).
1525pub const SECRET_SHOWN: &str = "<secret>";
1526
1527/// `text` with each `[[notify.targets]]` entry's `url` and header values shown as [`SECRET_SHOWN`], everything else
1528/// as written: what `GET /v1/config` answers, so no client, the dashboard's browsers included, holds them. `None`
1529/// when `text` is not TOML.
1530pub fn mask_secrets(text: &str) -> Option<String> {
1531    let mut document: toml_edit::DocumentMut = text.parse().ok()?;
1532    let mut masked = false;
1533    for target in notify_targets(&mut document) {
1534        for (_, value) in secrets_of(target) {
1535            replace_value(value, toml_edit::Value::from(SECRET_SHOWN));
1536            masked = true;
1537        }
1538    }
1539    Some(if masked {
1540        document.to_string()
1541    } else {
1542        text.to_owned()
1543    })
1544}
1545
1546/// `text` with each [`SECRET_SHOWN`] in a target's `url` or headers replaced by the value `stored` has for the target
1547/// of the same name, everything else as written: what `PUT /v1/config` and `POST /v1/config/check` read, so a config
1548/// shown masked saves with its secrets. A value with nothing to keep stays [`SECRET_SHOWN`], which `Config::validate`
1549/// refuses. A text that does not parse as a config is answered as sent: a TOML error quotes the line it is on, and
1550/// that line must hold the mask, not the value put back. One that parses still does with the values put back, as they
1551/// are strings where strings were, and `Config::validate` names a target's field, never its value.
1552pub fn keep_secrets(text: &str, stored: &str) -> String {
1553    if !text.contains(SECRET_SHOWN) || Config::parse(text).is_err() {
1554        return text.to_owned();
1555    }
1556    let (Ok(mut document), Ok(mut old)) = (
1557        text.parse::<toml_edit::DocumentMut>(),
1558        stored.parse::<toml_edit::DocumentMut>(),
1559    ) else {
1560        return text.to_owned();
1561    };
1562    let mut kept: BTreeMap<String, Vec<(SecretField, toml_edit::Value)>> = BTreeMap::new();
1563    for target in notify_targets(&mut old) {
1564        let Some(name) = target_name(target) else {
1565            continue;
1566        };
1567        let secrets = secrets_of(target)
1568            .into_iter()
1569            .map(|(field, value)| (field, value.clone()))
1570            .collect();
1571        kept.insert(name, secrets);
1572    }
1573    for target in notify_targets(&mut document) {
1574        let Some(secrets) = target_name(target).and_then(|name| kept.get(&name)) else {
1575            continue;
1576        };
1577        for (field, value) in secrets_of(target) {
1578            if value.as_str() != Some(SECRET_SHOWN) {
1579                continue;
1580            }
1581            if let Some((_, old)) = secrets.iter().find(|(kept, _)| *kept == field) {
1582                replace_value(value, old.clone());
1583            }
1584        }
1585    }
1586    document.to_string()
1587}
1588
1589/// Where a target keeps a secret.
1590#[derive(Debug, PartialEq, Eq)]
1591enum SecretField {
1592    Url,
1593    Header(String),
1594}
1595
1596/// The tables of `[[notify.targets]]`, or of an inline `targets = [...]`.
1597fn notify_targets(document: &mut toml_edit::DocumentMut) -> Vec<&mut dyn toml_edit::TableLike> {
1598    match document
1599        .get_mut("notify")
1600        .and_then(|notify| notify.get_mut("targets"))
1601    {
1602        Some(toml_edit::Item::ArrayOfTables(tables)) => tables
1603            .iter_mut()
1604            .map(|table| table as &mut dyn toml_edit::TableLike)
1605            .collect(),
1606        Some(toml_edit::Item::Value(toml_edit::Value::Array(array))) => array
1607            .iter_mut()
1608            .filter_map(toml_edit::Value::as_inline_table_mut)
1609            .map(|table| table as &mut dyn toml_edit::TableLike)
1610            .collect(),
1611        _ => Vec::new(),
1612    }
1613}
1614
1615fn target_name(target: &dyn toml_edit::TableLike) -> Option<String> {
1616    target
1617        .get("name")
1618        .and_then(toml_edit::Item::as_str)
1619        .map(str::to_owned)
1620}
1621
1622/// A target's `url` and header values.
1623fn secrets_of(target: &mut dyn toml_edit::TableLike) -> Vec<(SecretField, &mut toml_edit::Value)> {
1624    let mut found = Vec::new();
1625    for (key, item) in target.iter_mut() {
1626        match key.get() {
1627            "url" => found.extend(item.as_value_mut().map(|value| (SecretField::Url, value))),
1628            "headers" => {
1629                let Some(headers) = item.as_table_like_mut() else {
1630                    continue;
1631                };
1632                for (name, item) in headers.iter_mut() {
1633                    let field = SecretField::Header(name.get().to_owned());
1634                    found.extend(item.as_value_mut().map(|value| (field, value)));
1635                }
1636            }
1637            _ => {}
1638        }
1639    }
1640    found
1641}
1642
1643/// Puts `new` in place of `value`, with the spaces and comment around it as they were.
1644fn replace_value(value: &mut toml_edit::Value, mut new: toml_edit::Value) {
1645    *new.decor_mut() = value.decor().clone();
1646    *value = new;
1647}
1648
1649/// A config text's revision: a hash of its bytes (64-bit FNV-1a, in hex), the same in every runnerd. `PUT
1650/// /v1/config?revision=` refuses a text sent against another revision than the daemon's.
1651pub fn revision(text: &str) -> String {
1652    let hash = text.bytes().fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
1653        (hash ^ u64::from(byte)).wrapping_mul(0x0100_0000_01b3)
1654    });
1655    format!("{hash:016x}")
1656}
1657
1658/// Reads a slice's `MemoryMax` from systemd; `None` when it is unlimited.
1659pub fn slice_memory_max(slice: &str) -> std::io::Result<Option<ByteSize>> {
1660    let output = Command::new("systemctl")
1661        .args(["show", slice, "--property=MemoryMax", "--value"])
1662        .output()?;
1663    if !output.status.success() {
1664        return Err(std::io::Error::other(format!(
1665            "systemctl show {slice} failed: {}",
1666            String::from_utf8_lossy(&output.stderr).trim()
1667        )));
1668    }
1669    let value = String::from_utf8_lossy(&output.stdout);
1670    parse_memory_max(value.trim()).ok_or_else(|| {
1671        std::io::Error::other(format!(
1672            "systemctl show {slice}: unexpected MemoryMax {value:?}"
1673        ))
1674    })
1675}
1676
1677/// The CPU budget, in millicores, of `millicores` of CPUs overcommitted `overcommit` times.
1678pub fn cpu_budget(millicores: u64, overcommit: f64) -> u64 {
1679    #[expect(
1680        clippy::cast_precision_loss,
1681        clippy::cast_possible_truncation,
1682        clippy::cast_sign_loss,
1683        reason = "a host's millicores are far below 2^52, and the config check holds the factor above 0 and at \
1684                  most 16"
1685    )]
1686    let budget = (millicores as f64 * overcommit.max(0.0)).round() as u64;
1687    budget
1688}
1689
1690/// Reads the CPUs a slice's processes may use from systemd, in millicores: its `EffectiveCPUs` (or `AllowedCPUs`,
1691/// else the host's CPUs), less when its `CPUQuota` is lower.
1692pub fn slice_cpus(slice: &str) -> std::io::Result<u64> {
1693    let output = Command::new("systemctl")
1694        .args([
1695            "show",
1696            slice,
1697            "--property=EffectiveCPUs",
1698            "--property=AllowedCPUs",
1699            "--property=CPUQuotaPerSecUSec",
1700        ])
1701        .output()?;
1702    if !output.status.success() {
1703        return Err(std::io::Error::other(format!(
1704            "systemctl show {slice} failed: {}",
1705            String::from_utf8_lossy(&output.stderr).trim()
1706        )));
1707    }
1708    let host = std::thread::available_parallelism().map_or(1, std::num::NonZero::get) as u64;
1709    parse_slice_cpus(&String::from_utf8_lossy(&output.stdout), host).ok_or_else(|| {
1710        std::io::Error::other(format!(
1711            "systemctl show {slice}: unexpected CPU properties {:?}",
1712            String::from_utf8_lossy(&output.stdout).trim()
1713        ))
1714    })
1715}
1716
1717/// The CPUs, in millicores, that `systemctl show`'s `EffectiveCPUs`, `AllowedCPUs` and `CPUQuotaPerSecUSec` lines
1718/// leave a slice on a host with `host` CPUs; `None` when a value does not parse.
1719pub fn parse_slice_cpus(properties: &str, host: u64) -> Option<u64> {
1720    let value = |name: &str| {
1721        properties.lines().find_map(|line| {
1722            line.strip_prefix(name)
1723                .and_then(|rest| rest.strip_prefix('='))
1724                .map(str::trim)
1725        })
1726    };
1727    let set = [value("EffectiveCPUs"), value("AllowedCPUs")]
1728        .into_iter()
1729        .flatten()
1730        .find(|cpus| !cpus.is_empty());
1731    let cpus = match set {
1732        Some(set) => cpu_count(set)?,
1733        None => host,
1734    };
1735    let quota = match value("CPUQuotaPerSecUSec") {
1736        None | Some("" | "infinity") => u64::MAX,
1737        Some(quota) => quota_millicores(quota)?,
1738    };
1739    Some(cpus.saturating_mul(1000).min(quota))
1740}
1741
1742/// How many CPUs a set names, as systemd prints it (`0-3 8-11`) or as it is written (`0-3,8,10-11`).
1743fn cpu_count(set: &str) -> Option<u64> {
1744    set.split([',', ' '])
1745        .filter(|range| !range.is_empty())
1746        .try_fold(0u64, |count, range| {
1747            let (first, last) = range.split_once('-').unwrap_or((range, range));
1748            let (first, last): (u64, u64) = (first.parse().ok()?, last.parse().ok()?);
1749            Some(count + last.checked_sub(first)? + 1)
1750        })
1751}
1752
1753/// A `CPUQuotaPerSecUSec` as systemd prints a timespan, such as `800ms`, `2.500000s` or `1min 4s`, as the
1754/// millicores it allows: a second of CPU time each second is 1000.
1755fn quota_millicores(quota: &str) -> Option<u64> {
1756    // Each unit's length in milliseconds, as systemd's `format_timespan` names them.
1757    const UNITS: [(&str, f64); 10] = [
1758        ("y", 31_557_600_000.0),
1759        ("M", 2_629_800_000.0),
1760        ("w", 604_800_000.0),
1761        ("d", 86_400_000.0),
1762        ("h", 3_600_000.0),
1763        ("min", 60_000.0),
1764        ("s", 1000.0),
1765        ("ms", 1.0),
1766        ("us", 0.001),
1767        ("µs", 0.001),
1768    ];
1769    let millicores = quota.split_whitespace().try_fold(0.0, |total, part| {
1770        let at = part.find(|c: char| !(c.is_ascii_digit() || c == '.'))?;
1771        let (number, unit) = part.split_at(at);
1772        let per_unit = UNITS
1773            .iter()
1774            .find_map(|(name, per_unit)| (*name == unit).then_some(*per_unit))?;
1775        Some(total + number.parse::<f64>().ok()? * per_unit)
1776    })?;
1777    #[allow(
1778        clippy::cast_possible_truncation,
1779        clippy::cast_sign_loss,
1780        reason = "a quota is a few thousand millicores per CPU, checked finite and not negative first"
1781    )]
1782    (!quota.trim().is_empty() && millicores.is_finite() && millicores >= 0.0)
1783        .then(|| millicores.round() as u64)
1784}
1785
1786/// Parses a `MemoryMax` value as `systemctl show` prints it: bytes, or `infinity`.
1787pub fn parse_memory_max(value: &str) -> Option<Option<ByteSize>> {
1788    if value == "infinity" {
1789        Some(None)
1790    } else {
1791        value.parse().ok().map(|bytes| Some(ByteSize(bytes)))
1792    }
1793}
1794
1795/// A size in bytes, written with a binary unit as Docker and systemd do: `512M`, `9G`, `32.5G`.
1796#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
1797pub struct ByteSize(pub u64);
1798
1799const UNITS: [(char, u64); 4] = [
1800    ('T', 1 << 40),
1801    ('G', 1 << 30),
1802    ('M', 1 << 20),
1803    ('K', 1 << 10),
1804];
1805
1806impl FromStr for ByteSize {
1807    type Err = String;
1808
1809    #[allow(
1810        clippy::cast_precision_loss,
1811        clippy::cast_possible_truncation,
1812        clippy::cast_sign_loss,
1813        reason = "sizes in a config are far below 2^52 bytes, and the result is checked against u64's range first"
1814    )]
1815    fn from_str(s: &str) -> Result<Self, Self::Err> {
1816        let invalid = || {
1817            format!(
1818                "invalid size {s:?}, expected a number and a unit (K, M, G or T), such as \"9G\""
1819            )
1820        };
1821        let unit = s.chars().last().ok_or_else(invalid)?.to_ascii_uppercase();
1822        let (_, factor) = UNITS.iter().find(|(u, _)| *u == unit).ok_or_else(invalid)?;
1823        let number = &s[..s.len() - 1];
1824        if number.is_empty() || !number.chars().all(|c| c.is_ascii_digit() || c == '.') {
1825            return Err(invalid());
1826        }
1827        let number: f64 = number.parse().map_err(|_| invalid())?;
1828        let bytes = (number * *factor as f64).round();
1829        // `u64::MAX as f64` is 2^64, the first value out of range.
1830        if bytes >= u64::MAX as f64 {
1831            return Err(format!("size {s:?} is too large"));
1832        }
1833        Ok(ByteSize(bytes as u64))
1834    }
1835}
1836
1837impl fmt::Display for ByteSize {
1838    #[allow(
1839        clippy::cast_precision_loss,
1840        reason = "shown with two decimals, far coarser than f64's precision"
1841    )]
1842    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1843        let Some((unit, factor)) = UNITS.iter().find(|(_, factor)| self.0 >= *factor) else {
1844            return write!(f, "{}B", self.0);
1845        };
1846        let number = format!("{:.2}", self.0 as f64 / *factor as f64);
1847        let number = number.trim_end_matches('0').trim_end_matches('.');
1848        write!(f, "{number}{unit}")
1849    }
1850}
1851
1852impl ByteSize {
1853    /// The size as a config file writes it, reading back to the byte: `Display`'s form when that reads back as it,
1854    /// else the shortest exact decimal in one of the units, such as `1029.5G` where `Display` says `1.01T`. A size
1855    /// written from it, as auto sizing writes `memory`, is the size the config check then reads.
1856    #[allow(
1857        clippy::cast_precision_loss,
1858        reason = "a unit is a power of two, so the quotient is exact below 2^53 bytes; the parse checks it"
1859    )]
1860    pub fn exact(self) -> String {
1861        let reads_back = |text: &String| text.parse::<ByteSize>() == Ok(self);
1862        let shown = self.to_string();
1863        if reads_back(&shown) {
1864            return shown;
1865        }
1866        let mut shortest: Option<String> = None;
1867        for (unit, factor) in UNITS {
1868            let text = format!("{}{unit}", self.0 as f64 / factor as f64);
1869            if reads_back(&text) && shortest.as_ref().is_none_or(|best| text.len() < best.len()) {
1870                shortest = Some(text);
1871            }
1872        }
1873        shortest.unwrap_or(shown)
1874    }
1875
1876    /// The size rounded to the nearest, for a label too narrow for its `Display`: two significant digits below 10
1877    /// of its unit, whole units above, and the next unit up once it rounds to 1024: `3.3G` for 3.25G, `12G`,
1878    /// `512M`, and `1G` for 1023.5M. Every client's budget bar labels a block with it.
1879    #[allow(
1880        clippy::cast_precision_loss,
1881        reason = "shown with one decimal at most, far coarser than f64's precision"
1882    )]
1883    pub fn short(self) -> String {
1884        let Some(index) = UNITS.iter().position(|(_, factor)| self.0 >= *factor) else {
1885            return format!("{}B", self.0);
1886        };
1887        let (mut unit, factor) = UNITS[index];
1888        let mut value = self.0 as f64 / factor as f64;
1889        if value.round() >= 1024.0
1890            && let Some(larger) = index.checked_sub(1)
1891        {
1892            unit = UNITS[larger].0;
1893            value /= 1024.0;
1894        }
1895        // Halves round up, as `f64::round` does and formatting, which rounds them to even, does not.
1896        let tenths = (value * 10.0).round() / 10.0;
1897        if tenths < 10.0 {
1898            let number = format!("{tenths:.1}");
1899            format!("{}{unit}", number.trim_end_matches(".0"))
1900        } else {
1901            format!("{:.0}{unit}", value.round())
1902        }
1903    }
1904}
1905
1906impl<'de> Deserialize<'de> for ByteSize {
1907    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1908        String::deserialize(deserializer)?
1909            .parse()
1910            .map_err(de::Error::custom)
1911    }
1912}
1913
1914/// Parses a duration as Go and the registry write it, `30s`, `90m`, `168h`, or in days: `7d`.
1915pub fn parse_duration(s: &str) -> Result<Duration, String> {
1916    let invalid = || {
1917        format!(
1918            "invalid duration {s:?}, expected a number and s, m, h or d, such as \"168h\" or \"7d\""
1919        )
1920    };
1921    let (number, factor) = match s.chars().last() {
1922        Some('s') => (&s[..s.len() - 1], 1),
1923        Some('m') => (&s[..s.len() - 1], 60),
1924        Some('h') => (&s[..s.len() - 1], 3600),
1925        Some('d') => (&s[..s.len() - 1], 86400),
1926        _ => return Err(invalid()),
1927    };
1928    let number: u64 = number.parse().map_err(|_| invalid())?;
1929    number
1930        .checked_mul(factor)
1931        .map(Duration::from_secs)
1932        .ok_or_else(invalid)
1933}
1934
1935/// Parses an age as the clients' `--since` and `--older-than` take it: `30m`, `12h`, `7d`, `2w`.
1936pub fn parse_age(text: &str) -> Result<Duration, String> {
1937    let error = || "expected a number and s, m, h, d or w, such as 12h".to_owned();
1938    let split = text.find(|c: char| !c.is_ascii_digit()).ok_or_else(error)?;
1939    let (number, unit) = text.split_at(split);
1940    let number: u64 = number.parse().map_err(|_| error())?;
1941    let unit = match unit {
1942        "s" => 1,
1943        "m" => 60,
1944        "h" => 3600,
1945        "d" => 86_400,
1946        "w" => 7 * 86_400,
1947        _ => return Err(error()),
1948    };
1949    Ok(Duration::from_secs(number.saturating_mul(unit)))
1950}
1951
1952fn duration<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Duration, D::Error> {
1953    parse_duration(&String::deserialize(deserializer)?).map_err(de::Error::custom)
1954}
1955
1956#[cfg(test)]
1957mod tests;