tilo-actions 0.5.1

The Tilo action model: resources, the actions on them, and their registry, with the plain resource vocabulary that needs no store.
Documentation
//! Where an admitted offloadable invocation runs.
//!
//! An action whose contract declares [`Offload`] is a pure transform of
//! content: its inputs are the content its invocation names, its outputs are
//! content, and they depend on the inputs alone. The action API admits such
//! an invocation exactly as any other — who may invoke never changes — and
//! then asks the device's [`ActionOffload`] whether another of the person's
//! devices should run it. When one did, the API answers the invocation with
//! that output ([`Offloaded::Ran`]); otherwise, for any reason at all, the
//! app's own handler runs it here ([`Offloaded::RunHere`]), so a failed or
//! unreachable worker only ever costs time, never a different answer.
//!
//! The inputs a job may read are only the content the invocation's own
//! content parameters name ([`content_inputs`]), as the invoker's workspace
//! reaches it; never a path, an environment or an argument vector.

use std::future::Future;
use std::pin::Pin;
use std::time::Duration;

use crate::{
    ARGUMENTS_JSON_PREFIX, ActionAddress, ActionOutput, ActionParam, Offload, named_reference,
    remote::InvokeRequest,
};

/// One admitted invocation of an offloadable action.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OffloadRequest {
    /// The workspace it was invoked in.
    pub workspace: String,
    pub app_id: String,
    /// The invoked action.
    pub address: ActionAddress,
    /// The contract's declaration: what the work needs of a device.
    pub offload: Offload,
    /// Its inputs: the content hashes its arguments name, in order.
    pub inputs: Vec<String>,
}

/// What became of an offload request.
#[derive(Clone, Debug, PartialEq)]
pub enum Offloaded {
    /// Another device ran it; this is what a local run returns.
    Ran {
        output: ActionOutput,
        /// The device that ran it, as the offloader names devices.
        device: String,
        /// Wall time from the request to the output.
        elapsed: Duration,
    },
    /// Run it here, through the app's handler, and why.
    RunHere {
        /// Why it was not offloaded: no better-placed device, or what
        /// failed remotely.
        reason: String,
    },
}

/// A boxed, sendable future, as [`ActionOffload`] answers.
pub type OffloadFuture<'a> = Pin<Box<dyn Future<Output = Offloaded> + Send + 'a>>;

/// The device's route for offloadable invocations.
pub trait ActionOffload: Send + Sync {
    /// Run `request` on the best-placed other device, or say why it should
    /// run here.
    fn offload(&self, request: OffloadRequest) -> OffloadFuture<'_>;

    /// [`Self::offload`] for the invocation `request` was admitted from, as
    /// this device's API would send it to another device: a route that runs
    /// it elsewhere sends the invocation the person made, so the device that
    /// runs it answers that one and a retry there is not a second run. A
    /// route with no device to send it to keeps its own way of running it.
    fn offload_invocation(
        &self,
        request: OffloadRequest,
        invocation: InvokeRequest,
    ) -> OffloadFuture<'_> {
        let _ = invocation;
        self.offload(request)
    }
}

/// The content an invocation's `arguments` name through the content
/// parameters among `params` ([`ActionParam::is_content`],
/// [`ActionParam::is_content_list`]), in declaration order: a `tilo.content:`
/// address, or the reference alone. Each is named by its address, which is
/// what the caller's workspace keeps and hands another device, whether or not
/// the record that names it keeps the content sealed
/// ([`content_address`](tilo_content::content_address)).
pub fn content_inputs(
    params: &[ActionParam],
    arguments: &serde_json::Map<String, serde_json::Value>,
) -> Result<Vec<String>, String> {
    let address = |name: &str, value: &serde_json::Value| -> Result<String, String> {
        let text = value
            .as_str()
            .ok_or_else(|| format!("`{name}` is not a content address"))?;
        named_reference(text)
            .and_then(tilo_content::content_address)
            .ok_or_else(|| format!("`{name}` is not a content address"))
    };
    let mut inputs = Vec::new();
    for param in params {
        let Some(value) = arguments
            .get(param.name.as_ref())
            .filter(|value| !value.is_null())
        else {
            continue;
        };
        if param.is_content() {
            inputs.push(address(&param.name, value)?);
        } else if param.is_content_list() {
            let items = value
                .as_array()
                .ok_or_else(|| format!("`{}` is not a list of content", param.name))?;
            for item in items {
                inputs.push(address(&param.name, item)?);
            }
        }
    }
    Ok(inputs)
}

/// [`content_inputs`] of an invocation's wire `argument`: its named
/// arguments ([`ARGUMENTS_JSON_PREFIX`]), or none.
pub fn argument_inputs(
    params: &[ActionParam],
    argument: Option<&str>,
) -> Result<Vec<String>, String> {
    let Some(argument) = argument else {
        return Ok(Vec::new());
    };
    let json = argument
        .strip_prefix(ARGUMENTS_JSON_PREFIX)
        .ok_or("the invocation's arguments are not named")?;
    let arguments: serde_json::Map<String, serde_json::Value> =
        serde_json::from_str(json).map_err(|error| format!("unreadable arguments: {error}"))?;
    content_inputs(params, &arguments)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ParamType;

    #[test]
    fn the_inputs_are_the_content_the_content_parameters_name_in_order() {
        let a = "a".repeat(64);
        let b = "b".repeat(64);
        let params = vec![
            ActionParam::required("images", ParamType::Array, "Images").as_content_list(),
            ActionParam::optional("label", ParamType::Text, "A label"),
            ActionParam::optional("cover", ParamType::Text, "Cover").as_content(),
        ];
        let arguments = serde_json::json!({
            "images": [a, format!("tilo.content:{b}")],
            "label": "not content",
            "cover": a,
        });
        let arguments = arguments.as_object().unwrap();
        assert_eq!(
            content_inputs(&params, arguments),
            Ok(vec![a.clone(), b, a])
        );
        assert!(params[0].is_content_list() && !params[0].is_content());
        assert!(params[2].is_content() && !params[2].is_content_list());

        let wire = format!(
            "{ARGUMENTS_JSON_PREFIX}{}",
            serde_json::to_string(arguments).unwrap()
        );
        assert_eq!(
            argument_inputs(&params, Some(&wire)),
            content_inputs(&params, arguments)
        );
        assert_eq!(argument_inputs(&params, None), Ok(Vec::new()));
        assert!(argument_inputs(&params, Some("free text")).is_err());

        // Content the record keeps sealed is handed over by the address the
        // workspace keeps it under, as bare content already was.
        let sealed = tilo_content::ContentStore::memory()
            .sealing()
            .put(b"a photograph")
            .unwrap();
        assert!(tilo_content::is_sealed(&sealed), "{sealed}");
        let address = tilo_content::content_address(&sealed).unwrap();
        let arguments = serde_json::json!({
            "images": [sealed],
            "cover": format!("tilo.content:{sealed}"),
        });
        assert_eq!(
            content_inputs(&params, arguments.as_object().unwrap()),
            Ok(vec![address.clone(), address])
        );

        // A path is never content.
        let arguments = serde_json::json!({ "images": ["/home/someone/secret.png"] });
        assert!(content_inputs(&params, arguments.as_object().unwrap()).is_err());
    }
}