From f933e51f38791a801cc40edd750baf2258d76d48 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 16:03:52 +0100 Subject: [PATCH] feat(ci): parse checks.toml The CI plugin needs to know which checks a snapshot defines. This adds the `apps/origin-ci` crate, starting with `checks.toml`: each check's command, its inputs (the whole snapshot by default), a toolchain command whose output identifies its tools, and whether it's external. Each check issues statements of the kind `ci.`. `checks.toml` is always one of a check's inputs, read from where the checks are in force (`Defined`): the snapshot's own copy for `origin ci run`, and for a worker, the target's, as a target path. So a result never shares the input digest of one that ran another definition. --- Cargo.lock | 9 ++ Cargo.toml | 1 + apps/origin-ci/Cargo.toml | 16 ++ apps/origin-ci/src/checks.rs | 275 +++++++++++++++++++++++++++++++++++ apps/origin-ci/src/lib.rs | 5 + 5 files changed, 306 insertions(+) create mode 100644 apps/origin-ci/Cargo.toml create mode 100644 apps/origin-ci/src/checks.rs create mode 100644 apps/origin-ci/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 152fc092..3e13bcc4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -818,6 +818,15 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "origin-ci" +version = "0.1.0" +dependencies = [ + "origin-shared", + "serde", + "toml", +] + [[package]] name = "origin-client" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index fdcb89a0..6402d796 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] resolver = "3" members = [ + "apps/origin-ci", "apps/origin-src", "crates/origin-client", "crates/origin-platform", diff --git a/apps/origin-ci/Cargo.toml b/apps/origin-ci/Cargo.toml new file mode 100644 index 00000000..cf505ac5 --- /dev/null +++ b/apps/origin-ci/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "origin-ci" +description = "origin's CI plugin: runs the checks of checks.toml and issues their results" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true + +[dependencies] +origin-shared.workspace = true +serde.workspace = true +toml.workspace = true + +[lints] +workspace = true diff --git a/apps/origin-ci/src/checks.rs b/apps/origin-ci/src/checks.rs new file mode 100644 index 00000000..a1157d3d --- /dev/null +++ b/apps/origin-ci/src/checks.rs @@ -0,0 +1,275 @@ +//! `checks.toml`, at the root of a snapshot: the checks to run on it, or +//! for a worker, at the tip of a target, on what lands on it. +//! +//! ```toml +//! [checks.lint] +//! command = "cargo clippy --workspace -- -D warnings" +//! inputs = ["src", "Cargo.toml", "Cargo.lock"] +//! toolchain = "rustc -V" +//! +//! [checks.links] +//! command = "lychee docs" +//! inputs = ["docs"] +//! external = true # reads the network: never promoted +//! ``` +//! +//! Each check issues statements of the kind `ci.`. Its `inputs` are +//! the files and directories it reads, the whole snapshot by default; its +//! `toolchain`, a command whose output identifies the tools it runs with. +//! A result is promoted to every revision where the inputs, the command and +//! the toolchain's output are the same, unless the check is `external`. +//! +//! `checks.toml` is always one of the inputs, read from where the checks +//! are in force: see [`Defined`]. The landing queue promotes results by +//! input digest alone, without reading the checks, so a result must never +//! share the digest of one that ran another definition. A worker runs the +//! checks a target defines, so its results declare the file as the state +//! of the target the record is on holds it, never the record's own copy: a +//! candidate that restores an older `checks.toml` doesn't match the results +//! of the older definition, and one that only edits it still matches those +//! of the definition in force. Nor does the queue promote a result from +//! one target to another, whose checks may differ. +//! +//! **Known limitation.** The toolchain's output a result declares is the +//! one it ran with: the queue never runs the toolchain command, so tools +//! upgraded where checks run, with the inputs unchanged, don't stop the +//! old results from being promoted. Pin the tools in a file the check +//! reads instead, such as `rust-toolchain.toml` among its `inputs`: then +//! a new toolchain is a change of the inputs, and the check runs again. + +use std::collections::BTreeMap; + +use origin_shared::{Digest, Inputs, InvalidName, Kind, RepoPath}; +use serde::Deserialize; + +/// Where the `checks.toml` a check is defined by is in force, which its +/// results declare it read. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Defined { + /// In the snapshot checked, as `origin ci run` reads it: one of the + /// paths read from the snapshot. + InSnapshot, + /// On the target the record checked is on, as a worker reads it: one + /// of the paths read from the target's state. + OnTarget, +} + +/// The checks of a snapshot, by kind. +#[derive(Debug, Default, PartialEq, Eq, Deserialize)] +#[serde(try_from = "ChecksFile")] +pub struct Checks(BTreeMap); + +/// One check. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Check { + /// The kind of the statements it issues: `ci.`. + pub kind: Kind, + /// The shell command that runs it, in a checkout of the snapshot: it + /// passes if it exits with 0. + pub command: String, + /// The files and directories it reads: for a check + /// [defined](Defined::InSnapshot) in the snapshot, `checks.toml` + /// included. + pub inputs: Vec, + /// Those it reads from the state of the target the record is on: for a + /// check [defined](Defined::OnTarget) there, `checks.toml`. + pub target_inputs: Vec, + /// The shell command whose output identifies its tools, if any. + pub toolchain: Option, + /// Whether it also reads what isn't in the snapshot, such as the + /// network, so its results are never promoted. + pub external: bool, +} + +impl Checks { + /// The file holding the checks, at the root of a snapshot. + pub const FILE: &str = "checks.toml"; + + /// The namespace of the kinds checks issue. + pub const NAMESPACE: &str = "ci"; + + /// Parses `checks.toml`, in force where `defined` says, which each + /// check declares among its inputs. + /// + /// # Errors + /// + /// Fails if it isn't valid, or a check's name doesn't make a kind. + pub fn parse(text: &str, defined: Defined) -> Result { + let mut checks: Self = toml::from_str(text)?; + let file = Self::path(); + for check in checks.0.values_mut() { + match defined { + Defined::InSnapshot + if !check.inputs.iter().any(|input| file.starts_with(input)) => + { + check.inputs.push(file.clone()); + } + Defined::InSnapshot => {} + Defined::OnTarget => check.target_inputs.push(file.clone()), + } + } + Ok(checks) + } + + /// [`Checks::FILE`], as a path. + fn path() -> RepoPath { + RepoPath::new(Self::FILE).expect("a valid path") + } + + /// The check issuing `kind`, if there's one. + #[must_use] + pub fn get(&self, kind: &Kind) -> Option<&Check> { + self.0.get(kind) + } + + /// Every check, by kind. + pub fn iter(&self) -> impl Iterator { + self.0.values() + } + + /// The kind of the statements the check `name` issues: `ci.`. + /// + /// # Errors + /// + /// Fails if the name doesn't make a kind. + pub fn kind(name: &str) -> Result { + Kind::new(format!("{}.{name}", Self::NAMESPACE)) + } +} + +impl Check { + /// What a result of the check declares it depended on, with the output + /// of its toolchain command, if it has one. + #[must_use] + pub fn inputs(&self, toolchain: &[u8]) -> Inputs { + Inputs { + paths: self.inputs.clone(), + target_paths: self.target_inputs.clone(), + definition: Digest::of(self.command.as_bytes()), + toolchain: Digest::of(toolchain), + external: self.external, + } + } +} + +/// `checks.toml` as written. +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct ChecksFile { + #[serde(default)] + checks: BTreeMap, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct CheckFile { + command: String, + inputs: Option>, + toolchain: Option, + #[serde(default)] + external: bool, +} + +impl TryFrom for Checks { + type Error = InvalidName; + + fn try_from(file: ChecksFile) -> Result { + file.checks + .into_iter() + .map(|(name, check)| { + let kind = Self::kind(&name)?; + let check = Check { + kind: kind.clone(), + command: check.command, + inputs: check.inputs.unwrap_or_else(|| vec![RepoPath::root()]), + target_inputs: Vec::new(), + toolchain: check.toolchain, + external: check.external, + }; + Ok((kind, check)) + }) + .collect::>() + .map(Self) + } +} + +#[cfg(test)] +mod tests { + use std::slice; + + use super::*; + + const LINT_AND_LINKS: &str = "[checks.lint]\ncommand = \"cargo clippy\"\n\ + inputs = [\"src\", \"Cargo.toml\"]\ntoolchain = \"rustc -V\"\n\ + [checks.links]\ncommand = \"lychee docs\"\nexternal = true\n"; + + #[test] + fn parses_checks_by_kind() { + let checks = Checks::parse(LINT_AND_LINKS, Defined::InSnapshot).unwrap(); + let lint = checks.get(&Kind::new("ci.lint").unwrap()).unwrap(); + assert_eq!( + lint.inputs, + [ + RepoPath::new("src").unwrap(), + RepoPath::new("Cargo.toml").unwrap(), + RepoPath::new("checks.toml").unwrap(), + ], + "the checks are an input of each" + ); + assert_eq!(lint.target_inputs, []); + assert_eq!(lint.toolchain.as_deref(), Some("rustc -V")); + let links = checks.get(&Kind::new("ci.links").unwrap()).unwrap(); + assert_eq!(Checks::kind("links").unwrap(), links.kind); + assert_eq!(links.inputs, [RepoPath::root()], "the whole snapshot"); + assert!(links.external && links.inputs(b"").external); + assert_eq!(checks.iter().count(), 2); + + let parse = |text| Checks::parse(text, Defined::InSnapshot); + assert!( + parse("[checks.Lint]\ncommand = \"x\"\n").is_err(), + "not a kind" + ); + assert!( + parse("[checks.lint]\ncmd = \"x\"\n").is_err(), + "unknown key" + ); + assert_eq!(parse("").unwrap(), Checks::default()); + } + + #[test] + fn checks_in_force_on_a_target_read_it_there() { + let checks = Checks::parse(LINT_AND_LINKS, Defined::OnTarget).unwrap(); + let file = RepoPath::new(Checks::FILE).unwrap(); + for check in checks.iter() { + assert_eq!(check.target_inputs, slice::from_ref(&file), "{check:?}"); + assert!(!check.inputs.contains(&file), "{check:?}"); + } + let lint = checks.get(&Kind::new("ci.lint").unwrap()).unwrap(); + let in_snapshot = Checks::parse(LINT_AND_LINKS, Defined::InSnapshot).unwrap(); + let ran = lint.inputs(b"rustc 1.99.0"); + assert_eq!(ran.target_paths, [file]); + assert_ne!( + ran, + in_snapshot.get(&lint.kind).unwrap().inputs(b"rustc 1.99.0") + ); + } + + #[test] + fn inputs_follow_the_command_and_toolchain() { + let check = |command: &str| Check { + kind: Kind::new("ci.test").unwrap(), + command: command.to_owned(), + inputs: vec![RepoPath::new("src").unwrap()], + target_inputs: Vec::new(), + toolchain: None, + external: false, + }; + let inputs = check("cargo test").inputs(b"rustc 1.99.0"); + assert_eq!(inputs, check("cargo test").inputs(b"rustc 1.99.0")); + assert_ne!( + inputs, + check("cargo test --release").inputs(b"rustc 1.99.0") + ); + assert_ne!(inputs, check("cargo test").inputs(b"rustc 1.100.0")); + } +} diff --git a/apps/origin-ci/src/lib.rs b/apps/origin-ci/src/lib.rs new file mode 100644 index 00000000..31d1980d --- /dev/null +++ b/apps/origin-ci/src/lib.rs @@ -0,0 +1,5 @@ +//! `origin-ci`, origin's CI plugin: it runs the checks a revision's +//! `checks.toml` lists, and issues their results as statements, which the +//! landing queue takes as evidence. + +pub mod checks;