apoc 0.8.2

Agentic process control for LLMs
Documentation
use std::process::{Command, Output};

use serde_json::Value;
use tempfile::{TempDir, tempdir};

struct Fixture {
    binary: String,
    home: TempDir,
    runtime: TempDir,
}

impl Fixture {
    fn new() -> Self {
        Self {
            binary: env!("CARGO_BIN_EXE_apoc").to_string(),
            home: tempdir().unwrap(),
            runtime: tempdir().unwrap(),
        }
    }

    fn run(&self, args: &[&str]) -> Output {
        Command::new(&self.binary)
            .args(args)
            .args(["--purpose", "Running the code launch failure fixture."])
            .env("APOC_HOME", self.home.path())
            .env("APOC_RUNTIME_DIR", self.runtime.path())
            .env("APOC_VIEW", "full")
            .output()
            .unwrap()
    }
}

impl Drop for Fixture {
    fn drop(&mut self) {
        let _ = self.run(&[
            "daemon",
            "stop",
            "--idempotency-key",
            "code-launch-fixture-stop",
            "--format",
            "json",
        ]);
    }
}

#[test]
fn code_mode_returns_launch_failures_without_replaying_them() {
    let apoc = Fixture::new();
    let code = r#"
        const executions = await Promise.all([
            apoc.execution_command({
                executable: "/usr/bin/true",
                purpose: "Running the valid command before a launch failure.",
                idempotency_key: "code-launch-valid-before",
                name: "valid-before-launch-failure"
            }),
            apoc.execution_command({
                executable: "/definitely/missing-apoc-executable",
                purpose: "Checking a missing executable launch failure.",
                idempotency_key: "code-launch-missing",
                name: "missing-launch-failure"
            }),
            apoc.execution_command({
                executable: "/usr/bin/printf",
                purpose: "Running the valid command after a launch failure.",
                idempotency_key: "code-launch-valid-after",
                arg: ["ok"],
                name: "valid-after-launch-failure"
            })
        ]);
        return executions.map(({ execution: { id, status, result } }) => ({ id, status, result }));
    "#;
    let output = apoc.run(&[
        "code",
        "run",
        code,
        "--idempotency-key",
        "code-launch-failure-run",
        "--format",
        "json",
    ]);
    assert!(
        output.status.success(),
        "code run failed:\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    let state: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(state["status"], "completed", "{state:#}");
    assert_eq!(state["result"][0]["status"], "completed", "{state:#}");
    assert_eq!(state["result"][1]["status"], "failed", "{state:#}");
    assert_eq!(
        state["result"][1]["result"]["error_code"], "EXECUTION_START_FAILED",
        "{state:#}"
    );
    assert_eq!(state["result"][2]["status"], "completed", "{state:#}");

    let output = apoc.run(&["execution", "list", "--format", "json"]);
    assert!(
        output.status.success(),
        "execution list failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let executions: Value = serde_json::from_slice(&output.stdout).unwrap();
    let missing = executions
        .get("executions")
        .and_then(Value::as_array)
        .unwrap()
        .iter()
        .filter(|execution| execution["name"] == "missing-launch-failure")
        .collect::<Vec<_>>();
    assert_eq!(missing.len(), 1, "{executions:#}");
    assert_eq!(missing[0]["status"], "failed", "{executions:#}");
    assert_eq!(missing[0]["parent_id"], state["id"], "{executions:#}");
}