aion-server 0.29.0

Aion workflow server library: HTTP, gRPC, WebSocket, and worker endpoints. Run it with the `aion` binary from the aion-cli crate.
Documentation
use crate::assistant::EMBEDDED_ASSISTANT_DOCUMENT;

use super::{HarnessQueueError, harness_queues};

type TestResult = Result<(), Box<dyn std::error::Error>>;

/// The document this reads is the one the server actually ships and actually
/// deploys — a hand-written fixture would be a second document that could
/// drift from the real one without anything noticing.
const HARNESS_BLOCK: &str = "  harness\n";

/// The embedded assistant's own `harness` section, verbatim, as the fixtures
/// below cut it about.
fn assistant_harness() -> Result<(usize, usize), Box<dyn std::error::Error>> {
    let start = EMBEDDED_ASSISTANT_DOCUMENT
        .find(HARNESS_BLOCK)
        .ok_or("the embedded assistant must carry a harness section")?;
    let end = EMBEDDED_ASSISTANT_DOCUMENT[start..]
        .find("\n  action ")
        .map(|offset| start.saturating_add(offset).saturating_add(1))
        .ok_or("the harness section must be followed by an action")?;
    Ok((start, end))
}

/// 🔴 THE NEGATIVE CONTROL. Auto-provision keys on the `harness` section, not
/// on deploys. The SAME document with its section removed declares no queue —
/// nothing to mint, and no error either.
#[test]
fn a_document_with_no_harness_section_declares_no_queue() -> TestResult {
    let (start, end) = assistant_harness()?;
    let mut stripped = String::from(&EMBEDDED_ASSISTANT_DOCUMENT[..start]);
    stripped.push_str(&EMBEDDED_ASSISTANT_DOCUMENT[end..]);
    assert!(
        harness_queues(&stripped)?.is_empty(),
        "a document with no harness section must mint nothing"
    );
    Ok(())
}

/// The control for the control: the UNMODIFIED document does declare a queue,
/// and its launch compiles — so the empty answer above is the section's
/// absence rather than a reader that never finds anything.
#[test]
fn the_shipped_assistant_declares_its_queue_and_its_launch_compiles() -> TestResult {
    let queues = harness_queues(EMBEDDED_ASSISTANT_DOCUMENT)?;
    let queue = queues.first().ok_or("the assistant queue must be read")?;
    assert_eq!(queues.len(), 1);
    assert_eq!(queue.task_queue, "assistant");
    assert!(queue.harness.concurrency > 0);
    Ok(())
}

/// An incomplete section is a REFUSAL naming its queue, not a silent skip: a
/// record minted from it would replay an argv the worker refuses on every
/// restart until the crash-loop budget parked it.
#[test]
fn an_incomplete_section_refuses_and_names_its_queue() -> TestResult {
    let (start, end) = assistant_harness()?;
    let section = &EMBEDDED_ASSISTANT_DOCUMENT[start..end];
    let concurrency = section
        .lines()
        .find(|line| line.trim_start().starts_with("concurrency "))
        .ok_or("the shipped section must declare a concurrency")?;
    let maimed = EMBEDDED_ASSISTANT_DOCUMENT.replace(&format!("{concurrency}\n"), "");
    match harness_queues(&maimed) {
        Err(HarnessQueueError::Harness {
            task_queue,
            message,
        }) => {
            assert_eq!(task_queue, "assistant");
            assert!(message.contains("concurrency"), "{message}");
            Ok(())
        }
        other => Err(format!("an incomplete section must refuse: {other:?}").into()),
    }
}

/// A queue whose every action carries a declared body has nothing an agent
/// could be dispatched — the server runs those from the deployed contract — so
/// no worker is minted, and the refusal names the actions that shut it out.
#[test]
fn a_queue_of_only_bodied_actions_has_nothing_to_serve() -> TestResult {
    let agent_action = "  action assistant(prompt: String) -> Reply\n    agent\n";
    assert!(
        EMBEDDED_ASSISTANT_DOCUMENT.contains(agent_action),
        "the shipped assistant must declare its agent action in the expected shape"
    );
    let bodyless = EMBEDDED_ASSISTANT_DOCUMENT.replace(agent_action, "");
    match harness_queues(&bodyless) {
        Err(HarnessQueueError::NoServiceableAction {
            task_queue,
            actions,
        }) => {
            assert_eq!(task_queue, "assistant");
            assert!(actions.contains("assistant_provision"), "{actions}");
            Ok(())
        }
        other => Err(format!("an all-bodied queue must refuse: {other:?}").into()),
    }
}

/// Source that does not parse is reported as such, not as "declares nothing".
/// Those two answers lead an operator to opposite acts, and the archived
/// document is provenance — a package can load while carrying source that no
/// longer parses.
#[test]
fn unparseable_source_is_a_parse_refusal_not_an_empty_answer() -> TestResult {
    match harness_queues("this is not an AWL document") {
        Err(HarnessQueueError::Parse { .. }) => Ok(()),
        other => Err(format!("unparseable source must refuse: {other:?}").into()),
    }
}