artifactize-core 0.25.1

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 std::{future::Future, time::Duration};

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

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

/// Includes a zero-bound initial read and failure recovery; no MCP call reaches 45 s.
const WAIT_CALL_LIMIT: Duration = Duration::from_secs(44);

#[derive(Serialize)]
#[serde(untagged)]
pub enum Output {
    Observation(runs::RunObservation),
    Result(Box<runs::RunResultPage>),
    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 = "type")]
        name: crate::config::ArtifactType,
    },
}

/// Chat does not expose project-level plugin installation or removal.
pub(crate) fn chat_tool(name: &str) -> bool {
    matches!(
        tool::ToolId::parse(name),
        Some(tool::ToolId::Verify(_) | tool::ToolId::Read(_))
    )
}

impl Call {
    /// Execute the same operation in an MCP transport or an in-process Chat session.
    /// Transports resolve their own scope before invoking this; a Chat scopes it to one worktree.
    pub async fn execute<F: Future<Output = Result<(), String>>>(
        self,
        context: &runs::Context,
        cancel: CancellationToken,
        progress: impl FnMut(runs::RunEvent) -> F,
    ) -> Result<Output, Error> {
        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).await?,
                ));
            }
        };
        Ok(match command {
            Verify::Start(args) => {
                let options = VerifyOptions {
                    jobs: args.jobs.map(|limit| limit.get() as usize),
                    modes: runs::RunModes {
                        speculate: args.speculate,
                    },
                    comment: args.comment,
                    ..Default::default()
                };
                Output::Observation(
                    runs::start(context, args.scope.selection(), &options, args.request_key)
                        .await?,
                )
            }
            Verify::Status(run) => Output::Observation(runs::status(context, &run).await?),
            Verify::Wait(args) => Output::Observation(
                tokio::time::timeout(
                    WAIT_CALL_LIMIT,
                    runs::wait(context, &args.run, Some(args.duration()), cancel),
                )
                .await
                .map_err(|_| Error::Timeout)??,
            ),
            Verify::Watch(run) => Output::Observation(
                tokio::time::timeout(
                    WAIT_CALL_LIMIT,
                    runs::watch(
                        context,
                        &run,
                        Some(Duration::from_secs(DEFAULT_WAIT_SECONDS)),
                        cancel,
                        progress,
                    ),
                )
                .await
                .map_err(|_| Error::Timeout)??,
            ),
            Verify::Result(args) => Output::Result(Box::new(
                runs::result(context, &args.run, args.cursor).await?,
            )),
            Verify::Cancel(run) => Output::Cancellation(runs::cancel(context, &run).await?),
        })
    }

    /// Whether success reports the identity of the Run this tool started, not an inspected Run.
    pub fn started_run(&self) -> bool {
        matches!(self.command, Command::Verify(Verify::Start(_)))
    }
}

impl Output {
    pub fn observed_run(&self) -> Option<&RunId> {
        match self {
            Self::Observation(observation) => Some(&observation.run),
            _ => None,
        }
    }
}