artifactize-core 0.33.0

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
Documentation
//! Protocol-neutral implementation of the public Verify and inspection tools.
mod catalog;
mod error;
mod input;
mod page;
mod read;
mod tool;

pub use catalog::{Hints, Tool, tools};
pub use error::{ArgumentsError, Error};
pub use input::Call;
pub use page::{CursorError, Page};

use serde::Serialize;
use tokio_util::sync::CancellationToken;

use crate::{project::VerifyOptions, runs, types::RunId};
use input::{Command, Inspection, Plugin, Read, Verify};

#[derive(Serialize)]
#[serde(untagged)]
pub enum Output {
    Observation(runs::RunObservation),
    Cancellation(runs::CancelAcknowledgement),
    Inspection(Page),
    Plugin(PluginResult),
}

#[derive(Serialize)]
#[serde(tag = "action", rename_all = "lowercase")]
pub enum PluginResult {
    Added(crate::config::plugins::PluginInfo),
    Removed {
        #[serde(rename = "namespace")]
        name: crate::config::PluginName,
    },
}

/// Whether `name` is one of the public tools, served or not.
pub fn is_tool(name: &str) -> bool {
    tool::ToolId::parse(name).is_some()
}

/// The Run's `run_show` JSON, read at once and whole, with managed secrets scrubbed.
pub async fn run_json(context: &runs::Context, run: &crate::types::RunId) -> Result<String, Error> {
    read::run(context, run).await
}

/// An initial action wait; a cursor only reads the next page, never starts another wait.
pub struct ActionWait {
    pub run: RunId,
    page: page::Query,
}

impl ActionWait {
    pub async fn wait(
        self,
        context: &runs::Context,
        bound: runs::ActionWaitBound,
        cancel: CancellationToken,
    ) -> Result<Page, Error> {
        let summary = runs::wait_for_action(context, &self.run, bound, cancel).await?;
        read::project_page(context, self.page, serde_json::to_string(&summary)?)
    }
}

impl Call {
    /// The existing Run this call names: the one `run_show` reads or `run_cancel` cancels.
    pub fn run(&self) -> Option<&RunId> {
        match &self.command {
            Command::Inspect(Inspection {
                read: Read::Run(show),
                ..
            }) => Some(&show.run),
            Command::Verify(Verify::Cancel(run)) => Some(run),
            _ => None,
        }
    }

    /// Extract an initial action wait so an asynchronous transport can own its observation.
    pub fn action_wait(&self) -> Option<ActionWait> {
        match &self.command {
            Command::Inspect(Inspection {
                read: Read::Run(show),
                page,
            }) if show.wait_for_action && page.is_initial() => Some(ActionWait {
                run: show.run.clone(),
                page: page.clone(),
            }),
            _ => None,
        }
    }

    /// Execute an operation in the scope resolved by its transport.
    pub async fn execute(
        self,
        context: &runs::Context,
        cancel: CancellationToken,
    ) -> Result<Output, Error> {
        if let Some(wait) = self.action_wait() {
            return Ok(Output::Inspection(
                wait.wait(context, runs::ActionWaitBound::Inline, cancel)
                    .await?,
            ));
        }
        let command = match self.command {
            Command::Verify(command) => command,
            Command::Plugin(plugin) => {
                use crate::config::plugins;
                let result = match plugin {
                    Plugin::Add(args) => PluginResult::Added(
                        plugins::add(context.repo(), &args.path).map_err(Error::Config)?,
                    ),
                    Plugin::Remove(args) => {
                        plugins::remove(context.repo(), &args.name).map_err(Error::Config)?;
                        PluginResult::Removed { name: args.name }
                    }
                };
                return Ok(Output::Plugin(result));
            }
            Command::Inspect(inspection) => {
                return Ok(Output::Inspection(
                    read::inspect(context, inspection, cancel).await?,
                ));
            }
        };
        Ok(match command {
            Verify::Start(args) => {
                let fingerprint = args.request_key.as_ref().map(|_| args.fingerprint());
                let options = VerifyOptions {
                    jobs: args.jobs.map(|limit| limit.get() as usize),
                    modes: runs::RunModes {
                        speculate: args.speculate,
                        ignore_gates: args.ignore_gates,
                    },
                    comment: args.comment,
                    profile_variables: args.profile_variables,
                    ..Default::default()
                };
                Output::Observation(
                    runs::start_with_args_hash(
                        context,
                        args.scope.selection(),
                        &options,
                        args.request_key,
                        fingerprint,
                    )
                    .await?,
                )
            }
            Verify::Cancel(run) => Output::Cancellation(runs::cancel(context, &run).await?),
        })
    }
}