Luigit
repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by admin

smith-harness/tests/lifecycle.rs

Raw
//! SMH-SPEC-PLUG0001: two independently loaded sandbox consumers coalesce continuation.
#[global_allocator]
static ALLOC: smith_alloc::Counting = smith_alloc::Counting;
#[path = "fixtures/lifecycle_support.rs"]
mod support;
#[test]
fn compiling_same_named_fixtures_preserves_each_artifact() {
    let root = std::env::temp_dir().join(format!(
        "smith-fixture-artifacts-{}",
        smith::id::SessionId::new()
    ));
    let source = include_str!("fixtures/lifecycle.rs");
    let first =
        support::compile_fixture(&root.join("first"), source, "lifecycle-artifact-ownership");
    let bytes = std::fs::read(&first).unwrap();
    let second = support::compile_fixture(
        &root.join("second"),
        &source.replace("\"continue\"", "\"second\""),
        "lifecycle-artifact-ownership",
    );
    assert_ne!(first, second, "each compilation must own its output path");
    assert_eq!(std::fs::read(first).unwrap(), bytes);
    assert_ne!(std::fs::read(second).unwrap(), bytes);
    std::fs::remove_dir_all(root).unwrap();
}

#[test]
fn two_components_ensure_exactly_one_next_request() {
    use smith::{
        id::MessageId,
        stream::{StopReason, StreamEvent},
    };
    use smith_harness::runtime::{RuntimeConfig, build_agent};
    let root = std::env::temp_dir().join(format!("smith-lifecycle-{}", std::process::id()));
    std::fs::create_dir_all(&root).unwrap();
    support::install_consumers(&root, &root.join("plugins"));
    let script = ["first", "continued"]
        .into_iter()
        .map(|text| {
            vec![
                StreamEvent::text_delta(MessageId::new(), text),
                StreamEvent::stop(StopReason::EndTurn, None),
            ]
        })
        .collect();
    let mut agent = build_agent(&RuntimeConfig::mock(&root, script)).unwrap();
    assert_eq!(agent.run_turn("root").unwrap().text, "continued");
    assert!(
        format!("{:?}", agent.session().projected_messages().unwrap()).contains("effective-root")
    );
    std::fs::remove_dir_all(root).unwrap();
}

fn dispatcher(root: &std::path::Path, source: &str) -> smith_harness::wasm::RuntimeToolsDispatcher {
    use smith_harness::{plugins::registry::PackageRegistry, wasm::PluginEngine};
    let artifact =
        support::compile_fixture(&root.join("build"), source, "lifecycle-boundary-fixture");
    let registry = PackageRegistry::at(root.join("plugins"));
    let engine = PluginEngine::new().unwrap();
    support::install(
        &registry,
        &engine,
        &root.join("package"),
        "example:decisions",
        &artifact,
    );
    smith_harness::wasm::runtime_tools_dispatcher(
        root,
        smith_ai::auth::AuthStore::at(root.join("auth")),
        false,
        None,
    )
    .with_package_registry(registry)
}
fn root(name: &str) -> std::path::PathBuf {
    let root = std::env::temp_dir().join(format!("smith-lifecycle-{name}-{}", std::process::id()));
    std::fs::create_dir_all(&root).unwrap();
    root
}
fn event(round_index: u32) -> smith::lifecycle::LifecycleEvent {
    smith::lifecycle::LifecycleEvent {
        root_entry_id: smith::EntryId::from_u128(1),
        boundary: smith::lifecycle::Boundary::TurnComplete,
        round_index,
        last_stop: smith::StopReason::EndTurn,
        continuation_eligible: true,
    }
}
fn failure_source(required: bool) -> String {
    include_str!("fixtures/lifecycle.rs")
        .replace("vec![HandlerDeclaration { name: \"continue\".into(), boundary: Boundary::TurnComplete, required_decision: false }]", &format!("vec![HandlerDeclaration {{ name: \"continue\".into(), boundary: Boundary::TurnComplete, required_decision: false }}, HandlerDeclaration {{ name: \"fail\".into(), boundary: Boundary::TurnComplete, required_decision: {required} }}]"))
        .replace("fn handle(_: String,", "fn handle(name: String,")
        .replace("Ok(if event.round_index", "if name == \"fail\" {{ return Err(\"private-guest-error\".into()); }} Ok(if event.round_index").replace("{{ return Err", "{ return Err").replace("; }} Ok", "; } Ok")
}
#[test]
fn optional_failure_disables_only_its_handler_for_current_generation() {
    use smith::lifecycle::{DiagnosticCode, LifecycleAction};
    use smith_core::tools::ToolDispatcher;
    let root = root("optional");
    let source = failure_source(false);
    let mut dispatcher = dispatcher(&root, &source);
    dispatcher.begin_provider_round().unwrap();
    let first = dispatcher
        .lifecycle(&event(0), &smith::CancelHandle::new())
        .unwrap();
    assert_eq!(first.actions, vec![LifecycleAction::EnsureContinuation]);
    assert_eq!(first.diagnostics.len(), 1);
    assert_eq!(first.diagnostics[0].code, DiagnosticCode::HandlerFailed);
    assert_eq!(first.diagnostics[0].handler, "fail");
    assert!(!first.required_failure);
    assert!(first.allocated_bytes > 0);
    let next = dispatcher
        .lifecycle(&event(0), &smith::CancelHandle::new())
        .unwrap();
    assert_eq!(next.actions, vec![LifecycleAction::EnsureContinuation]);
    assert!(next.diagnostics.is_empty());
    // A separately published generation does not inherit disablement.
    let mut replacement = self::dispatcher(&root, &source);
    replacement.begin_provider_round().unwrap();
    assert_eq!(
        replacement
            .lifecycle(&event(0), &smith::CancelHandle::new())
            .unwrap()
            .diagnostics
            .len(),
        1
    );
    std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn required_failure_discards_prior_handler_actions() {
    use smith_core::tools::ToolDispatcher;
    let root = root("required");
    let mut dispatcher = dispatcher(&root, &failure_source(true));
    dispatcher.begin_provider_round().unwrap();
    let batch = dispatcher
        .lifecycle(&event(0), &smith::CancelHandle::new())
        .unwrap();
    assert!(batch.required_failure);
    assert!(batch.actions.is_empty());
    assert_eq!(batch.diagnostics.len(), 1);
    assert_eq!(
        batch.diagnostics[0].code,
        smith::lifecycle::DiagnosticCode::RequiredDecisionFailed
    );
    std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn shared_fuel_exhaustion_disables_optional_handler_without_cancelling_root() {
    fuel_exhaustion(smith::lifecycle::Boundary::TurnComplete);
}

#[test]
fn settled_cleanup_keeps_the_same_shared_fuel_ceiling() {
    fuel_exhaustion(smith::lifecycle::Boundary::Settled);
}

fn fuel_exhaustion(boundary: smith::lifecycle::Boundary) {
    use smith_core::tools::ToolDispatcher;
    let root = root("fuel");
    let source = include_str!("fixtures/lifecycle.rs")
        .replace("Boundary::TurnComplete", &format!("Boundary::{boundary:?}"));
    let mut dispatcher = dispatcher(&root, &source).with_composition_grants(
        smith_harness::wasm::CompositionGrants::default()
            .with_execution_limits(1, std::time::Duration::from_secs(30))
            .unwrap(),
    );
    dispatcher.begin_provider_round().unwrap();
    let cancel = smith::CancelHandle::new();
    let event = smith::lifecycle::LifecycleEvent {
        boundary,
        ..event(0)
    };
    let batch = dispatcher.lifecycle(&event, &cancel).unwrap();
    assert!(!cancel.is_cancelled());
    assert!(!batch.required_failure);
    assert!(batch.actions.is_empty());
    assert_eq!(batch.diagnostics.len(), 1);
    assert_eq!(
        batch.diagnostics[0].code,
        smith::lifecycle::DiagnosticCode::BudgetExceeded
    );
    assert!(
        dispatcher
            .lifecycle(&event, &cancel)
            .unwrap()
            .diagnostics
            .is_empty()
    );
    std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn cancellation_action_survives_an_invalid_non_cancel_batch() {
    use smith_core::tools::ToolDispatcher;
    let root = root("cancel");
    let source = include_str!("fixtures/lifecycle.rs").replace(
        "vec![Action::EnsureContinuation]",
        "vec![Action::Cancel, Action::SetPrompt(\"a\".into()), Action::SetPrompt(\"b\".into())]",
    );
    let mut dispatcher = dispatcher(&root, &source);
    dispatcher.begin_provider_round().unwrap();
    let cancel = smith::CancelHandle::new();
    assert!(matches!(
        dispatcher.lifecycle(&event(0), &cancel),
        Err(smith::SmithError::Cancelled)
    ));
    assert!(cancel.is_cancelled());
    std::fs::remove_dir_all(root).unwrap();
}

#[cfg(unix)]
#[test]
fn failed_reload_retains_prior_subscriptions_and_disable_set_then_valid_reload_resets_them() {
    use smith_core::tools::ToolDispatcher;
    use smith_harness::{
        plugin_control::ReloadControl, plugins::registry::PackageRegistry, wasm::PluginEngine,
    };
    let root = root("reload");
    let source = failure_source(false);
    let artifact =
        support::compile_fixture(&root.join("build"), &source, "lifecycle-reload-fixture");
    let registry = PackageRegistry::at(root.join("plugins"));
    let engine = PluginEngine::new().unwrap();
    let package = root.join("package");
    support::install(&registry, &engine, &package, "example:decisions", &artifact);
    registry.link(&engine, &package, &[]).unwrap();
    let socket = root.join("private/control.sock");
    let control = ReloadControl::bind_at_path(socket.clone()).unwrap();
    let mut dispatcher = smith_harness::wasm::runtime_tools_dispatcher(
        &root,
        smith_ai::auth::AuthStore::at(root.join("auth")),
        false,
        Some(control),
    )
    .with_package_registry(registry);
    dispatcher.begin_provider_round().unwrap();
    let cancel = smith::CancelHandle::new();
    assert_eq!(
        dispatcher
            .lifecycle(&event(0), &cancel)
            .unwrap()
            .diagnostics
            .len(),
        1
    );
    fn reload(socket: &std::path::Path) {
        use std::io::{Read, Write};
        let mut stream = std::os::unix::net::UnixStream::connect(socket).unwrap();
        stream
            .set_read_timeout(Some(std::time::Duration::from_secs(2)))
            .unwrap();
        stream
            .set_write_timeout(Some(std::time::Duration::from_secs(2)))
            .unwrap();
        stream.write_all(b"reload\n").unwrap();
        let mut response = [0; 3];
        stream.read_exact(&mut response).unwrap();
        assert_eq!(&response, b"ok\n");
    }
    let invalid = source.replace("name: \"continue\".into()", "name: \"\".into()");
    let artifact =
        support::compile_fixture(&root.join("invalid"), &invalid, "lifecycle-invalid-fixture");
    std::fs::copy(artifact, package.join("fixture.wasm")).unwrap();
    reload(&socket);
    dispatcher.begin_provider_round().unwrap();
    let retained = dispatcher.lifecycle(&event(0), &cancel).unwrap();
    assert_eq!(
        retained.actions,
        vec![smith::lifecycle::LifecycleAction::EnsureContinuation]
    );
    assert!(retained.diagnostics.is_empty());
    let artifact = support::compile_fixture(
        &root.join("replacement"),
        &source,
        "lifecycle-valid-fixture",
    );
    std::fs::copy(artifact, package.join("fixture.wasm")).unwrap();
    reload(&socket);
    dispatcher.begin_provider_round().unwrap();
    let replacement = dispatcher.lifecycle(&event(0), &cancel).unwrap();
    assert_eq!(
        replacement.actions,
        vec![smith::lifecycle::LifecycleAction::EnsureContinuation]
    );
    assert_eq!(replacement.diagnostics.len(), 1);
    drop(dispatcher);
    std::fs::remove_dir_all(root).unwrap();
}

#[test]
fn lifecycle_tool_policy_cannot_grow_resident_state_past_its_item_bound() {
    use smith::lifecycle::{LifecycleAction, TOOL_POLICY_ITEMS_MAX, ToolAvailability};
    use smith_core::tools::ToolDispatcher;
    let root = root("policy-bound");
    let source = include_str!("fixtures/lifecycle.rs").replace("fn declare_tools() -> Vec<plugin::ToolDeclaration> {\n        vec![]", "fn declare_tools() -> Vec<plugin::ToolDeclaration> {\n        vec![plugin::ToolDeclaration { name: \"controlled\".into(), description: \"controlled fixture\".into(), input_schema_json: \"{}\".into() }]");
    let mut policy = smith_harness::wasm::ToolPolicy::default();
    for index in 0..TOOL_POLICY_ITEMS_MAX {
        policy
            .set(
                format!("prior:host/tool-{index}"),
                ToolAvailability::Disabled,
            )
            .unwrap();
    }
    let mut dispatcher = dispatcher(&root, &source).with_tool_policy(policy);
    dispatcher.begin_provider_round().unwrap();
    assert!(
        dispatcher
            .validate_lifecycle(
                &[LifecycleAction::SetToolPolicy(vec![(
                    "example:decisions/controlled".into(),
                    ToolAvailability::Hidden
                )])],
                "mock"
            )
            .is_err()
    );
    std::fs::remove_dir_all(root).unwrap();
}

#[test]
fn allocation_crossing_discards_actions_and_stops_later_handlers_without_cancelling_root() {
    use smith_core::tools::ToolDispatcher;
    for (name, first_action) in [
        ("allocation-continuation", "Action::EnsureContinuation"),
        ("allocation-cancel", "Action::Cancel"),
    ] {
        let root = root(name);
        // Second handler would cancel the root if entered after the first crossed the budget.
        let source = failure_source(false)
            .replace(
                "return Err(\"private-guest-error\".into());",
                "return Ok(vec![Action::Cancel]);",
            )
            .replace(
                "vec![Action::EnsureContinuation]",
                &format!("vec![{first_action}]"),
            );
        let mut dispatcher = dispatcher(&root, &source).with_composition_grants(
            smith_harness::wasm::CompositionGrants::default()
                .with_host_allocation_limit(1)
                .unwrap(),
        );
        dispatcher.begin_provider_round().unwrap();
        let cancel = smith::CancelHandle::new();
        let batch = dispatcher.lifecycle(&event(0), &cancel).unwrap();
        assert!(!cancel.is_cancelled());
        assert!(!batch.required_failure);
        assert!(batch.allocated_bytes > 1);
        assert!(batch.actions.is_empty());
        assert_eq!(batch.diagnostics.len(), 2);
        assert!(
            batch
                .diagnostics
                .iter()
                .all(|diagnostic| diagnostic.code
                    == smith::lifecycle::DiagnosticCode::BudgetExceeded)
        );
        assert!(
            dispatcher
                .lifecycle(&event(0), &cancel)
                .unwrap()
                .diagnostics
                .is_empty()
        );
        std::fs::remove_dir_all(root).unwrap();
    }
}

#[test]
fn real_lifecycle_guest_cannot_catch_intent_or_completion_ack_failure_into_root_settlement() {
    use futures::StreamExt;
    use smith::{
        StreamFn,
        stream::{StopReason, StreamEvent},
    };
    use smith_core::{
        agent::Agent,
        session::{EntryContent, Session},
        store::create_session,
        tools::ToolSession,
    };
    use smith_harness::{
        plugins::{Capability, NetScope, registry::PackageRegistry},
        wasm::PluginEngine,
    };
    use std::{
        io::{Read, Write},
        net::TcpListener,
        sync::{
            Arc, Mutex,
            atomic::{AtomicUsize, Ordering},
        },
    };
    for failed_kind in [
        smith_core::metadata::lifecycle::EFFECT_INTENT,
        smith_core::metadata::lifecycle::EFFECT_COMPLETION,
    ] {
        let root = root(if failed_kind.ends_with("intent") {
            "effect-intent-ack"
        } else {
            "effect-completion-ack"
        });
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let address = listener.local_addr().unwrap();
        let origin = format!("http://{address}");
        let source = include_str!("fixtures/lifecycle.rs")
            .replace("Boundary::TurnComplete", "Boundary::PreSettlement")
            .replace("vec![Action::EnsureContinuation]", &format!(r#"{{
                let request = smith::plugin::net_scope::HttpRequest {{
                    destination: "{origin}".into(), method: "POST".into(), headers: vec![],
                    body: b"fixture".to_vec(), options: smith::plugin::net_scope::RequestOptions {{
                        timeout_ms: 1000, response_bytes_max: 64 }},
                }};
                // Catching storage failure must not authorize another HTTP effect or root settlement.
                let _caught = smith::plugin::net_scope::request(&request);
                let _again = smith::plugin::net_scope::request(&request);
                vec![]
            }}"#))
            .replace("        use exports::smith::plugin::lifecycle::*;\n        Ok(if", "        Ok(if");
        let artifact =
            support::compile_fixture(&root.join("guest"), &source, "lifecycle-ack-fixture");
        let package = root.join("package");
        std::fs::create_dir_all(&package).unwrap();
        std::fs::copy(artifact, package.join("fixture.wasm")).unwrap();
        std::fs::write(package.join("plugin.kdl"),
            "id \"example:ack-boundary\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"fixture.wasm\"\ncapability \"net-scope\"\n").unwrap();
        let registry = PackageRegistry::at(root.join("plugins"));
        registry
            .install_with_net_scopes(
                &PluginEngine::new().unwrap(),
                &package,
                &[Capability::NetScope],
                &[NetScope::parse(&origin).unwrap()],
            )
            .unwrap();
        let dispatcher = smith_harness::wasm::runtime_tools_dispatcher(
            &root,
            smith_ai::auth::AuthStore::at(root.join("auth")),
            false,
            None,
        )
        .with_package_registry(registry);
        let path = root.join("session.smh");
        let mut opened =
            create_session(&path, &Session::new(), smith::Config::default_valid()).unwrap();
        let admission = opened
            .writer
            .admit(
                &smith::message::Message::with_text(smith::message::Role::User, "root"),
                None,
                None,
            )
            .unwrap();
        let session = opened.writer.claim(admission.submission_id).unwrap();
        let writer = Arc::new(Mutex::new(opened.writer));
        let append = writer.clone();
        let completion = writer.clone();
        let mut tools = ToolSession::with_persist(root.clone(), session, move |entry, bytes|
            append.lock().unwrap().append_entry(entry, bytes))
            .with_completion_boundary(admission.submission_id, move |frame| {
                if let smith_core::frame::Frame::Known { entry } = &frame
                    && matches!(EntryContent::decode(&entry.content).unwrap(), EntryContent::Meta { kind, .. } if kind == failed_kind) {
                    return Err(smith::SmithError::Cancelled);
                }
                completion.lock().unwrap().append_frame(&frame)
            });
        tools.set_dispatcher(Box::new(dispatcher));
        let stream: StreamFn = Arc::new(|_, _| {
            futures::stream::iter(vec![
                Ok(StreamEvent::text_delta(smith::MessageId::new(), "done")),
                Ok(StreamEvent::stop(StopReason::EndTurn, None)),
            ])
            .boxed()
        });
        let mut agent = Agent::new(stream, tools, "mock");
        let probe = listener.try_clone().unwrap();
        let server = if failed_kind.ends_with("completion") {
            Some(std::thread::spawn(move || {
                let (mut stream, _) = listener.accept().unwrap();
                stream
                    .set_read_timeout(Some(std::time::Duration::from_secs(5)))
                    .unwrap();
                let mut bytes = [0; 4096];
                assert!(stream.read(&mut bytes).unwrap() > 0);
                stream
                    .write_all(
                        b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok",
                    )
                    .unwrap();
            }))
        } else {
            None
        };
        let settlements = Arc::new(AtomicUsize::new(0));
        let settled = settlements.clone();
        let result = agent.run_admitted(admission.submission_id, move |frame| {
            settled.fetch_add(1, Ordering::Relaxed);
            writer.lock().unwrap().append_frame(&frame)
        });
        assert!(result.is_err());
        assert_eq!(settlements.load(Ordering::Relaxed), 0);
        assert!(agent.run_turn("must-not-retry").is_err());
        if let Some(server) = server {
            server.join().unwrap();
        } else {
            probe.set_nonblocking(true).unwrap();
            assert_eq!(
                probe.accept().unwrap_err().kind(),
                std::io::ErrorKind::WouldBlock,
                "failed intent acknowledgement must prevent any HTTP"
            );
        }
        let reader = smith_core::store::SessionReader::open(&path).unwrap();
        assert_eq!(
            reader
                .work_status(
                    admission.submission_id,
                    smith::Config::default_valid().frame_bytes_max
                )
                .unwrap(),
            Some(smith_core::store::WorkStatus::Running)
        );
        std::fs::remove_dir_all(root).unwrap();
    }
}