Luigit
repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by admin

smith-harness/tests/registered_provider.rs

Raw
//! Public registered provider consumer (`SMH-SPEC-SPEC0001`, `SMH-SPEC-PLUG0001`).
#[global_allocator]
static ALLOC: smith_alloc::Counting = smith_alloc::Counting;

use smith_harness::plugins::{Capability, PluginManifest};
use smith_harness::protocol::{
    ProtocolComponent, ProtocolConnection, ProtocolConnectionConfig, registered_protocol_transport,
};
use smith_harness::wasm::bindings::exports::smith::plugin::provider::{
    CatalogModel, CatalogSnapshot, CatalogSource, ModelCapabilities, ProviderSettings,
    SettingsProvenance,
};
use smith_harness::wasm::{PluginEngine, PluginGeneration, PluginRegistry};
use std::{
    path::{Path, PathBuf},
    time::Duration,
};

fn fixture(root: &Path) -> PathBuf {
    compile_fixture(
        root,
        include_str!("fixtures/registered-provider.rs"),
        "registered-provider-fixture",
    )
}

fn compile_fixture(root: &Path, source: &str, name: &str) -> PathBuf {
    std::fs::create_dir_all(root.join("wit")).unwrap();
    std::fs::write(
        root.join("wit/smith-plugin.wit"),
        include_str!("../../wit/smith-plugin.wit"),
    )
    .unwrap();
    std::fs::write(root.join("lib.rs"), source).unwrap();
    std::fs::write(
        root.join("Cargo.toml"),
        format!(
            r#"[package]
name = "{name}"
version = "0.0.0"
edition = "2024"
[lib]
path = "lib.rs"
crate-type = ["cdylib"]
[dependencies]
wit-bindgen = {{ version = "=0.62.0", default-features = false, features = ["macros"] }}
[workspace]
"#
        ),
    )
    .unwrap();
    let target = Path::new(env!("CARGO_MANIFEST_DIR")).join("../target");
    for args in [
        vec!["generate-lockfile", "--offline"],
        vec![
            "build",
            "--offline",
            "--locked",
            "--target",
            "wasm32-wasip2",
        ],
    ] {
        assert!(std::process::Command::new("cargo").args(args).current_dir(root).env_remove("CARGO_ENCODED_RUSTFLAGS").env("RUSTFLAGS", "-C target-cpu=generic -C link-arg=--initial-memory=2097152 -C link-arg=--max-memory=2097152").env("CARGO_TARGET_DIR", &target).env("XDG_CONFIG_HOME", root.join("config")).status().unwrap().success());
    }
    target.join(format!(
        "wasm32-wasip2/debug/{}.wasm",
        name.replace('-', "_")
    ))
}
fn generation(engine: &PluginEngine, artifact: &Path, id: &str) -> PluginGeneration {
    let manifest = PluginManifest::parse(&format!("id \"{id}\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"fixture.wasm\"\ncapability \"net-scope\"\ncapability \"secret-proxy\"")).unwrap();
    PluginGeneration::load(
        engine,
        manifest,
        artifact,
        &[Capability::NetScope, Capability::SecretProxy],
    )
    .unwrap()
}
fn config(
    root: &Path,
    endpoint: String,
    records: smith::provider::RecordSink,
) -> ProtocolConnectionConfig {
    ProtocolConnectionConfig {
        connection_id: "fixture-host",
        protocol: ProtocolComponent::Registered,
        artifact: None,
        endpoint,
        template_url: "https://fixture.invalid/infer",
        credential_header: "Authorization",
        credential_prefix: "Bearer ",
        static_headers: &[],
        connection: ProtocolConnection::Api,
        request_path: "/authorized".into(),
        credential: smith_ai::auth::CredentialReference::explicit("isolated-secret").into(),
        auth_store: smith_ai::auth::AuthStore::at(root),
        executor: smith_harness::http::default_executor(),
        records,
        intent_admission_timeout: Duration::from_secs(5),
        settings: ProviderSettings {
            include_usage: true,
            subscription: false,
            provenance: SettingsProvenance::Explicit,
        },
        catalog: CatalogSnapshot {
            generation: 41,
            source: CatalogSource::Cache,
            models: vec![CatalogModel {
                id: "fixture-model".into(),
                capabilities: ModelCapabilities {
                    output_tokens_max: 16,
                    tool_use: false,
                    thinking: false,
                },
            }],
        },
    }
}
#[test]
fn registered_component_runs_real_agent_with_frozen_identity_and_durable_effects() {
    use smith_core::{
        agent::Agent,
        store::{SessionReader, open_session},
        tools::ToolSession,
    };
    use std::io::{Read, Write};
    let root =
        std::env::temp_dir().join(format!("smith-registered-provider-{}", std::process::id()));
    std::fs::create_dir_all(&root).unwrap();
    let built = fixture(&root);
    let artifact = root.join("fixture.wasm");
    std::fs::copy(&built, &artifact).unwrap();
    let hash = smith::sha256::digest_hex(&std::fs::read(&artifact).unwrap());
    let engine = PluginEngine::new().unwrap();
    let mut registry = PluginRegistry::new();
    registry
        .publish_all(vec![
            generation(&engine, &artifact, "example:custom"),
            generation(&engine, &artifact, "example:other"),
        ])
        .unwrap();
    // Publication freezes the code before consumer selection, not merely dispatch.
    std::fs::write(&artifact, b"replaced after publication").unwrap();
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let endpoint = format!("http://{}", listener.local_addr().unwrap());
    let (records, drain) = smith::provider::record_channel();
    let cfg = config(&root, endpoint, records);
    let stream = registered_protocol_transport(
        &registry,
        "example:custom/custom",
        "example:custom/fixture-model",
        "example:custom/fixture-key",
        cfg.clone(),
    )
    .unwrap();
    assert!(
        registered_protocol_transport(
            &registry,
            "custom",
            "fixture-model",
            "fixture-key",
            cfg.clone()
        )
        .err()
        .unwrap()
        .to_string()
        .contains("ambiguous")
    );
    for (provider, model, source) in [
        ("missing", "fixture-model", "fixture-key"),
        (
            "example:custom/custom",
            "example:other/fixture-model",
            "example:custom/fixture-key",
        ),
        (
            "example:custom/custom",
            "example:custom/fixture-model",
            "example:other/fixture-key",
        ),
    ] {
        assert!(
            registered_protocol_transport(&registry, provider, model, source, cfg.clone()).is_err()
        );
    }
    let mut missing_catalog = cfg.clone();
    missing_catalog.catalog.models.clear();
    assert!(
        registered_protocol_transport(
            &registry,
            "example:custom/custom",
            "example:custom/fixture-model",
            "example:custom/fixture-key",
            missing_catalog
        )
        .is_err()
    );
    use futures::StreamExt;
    let request = smith::provider::ProviderRequest::new("fixture-model", vec![]);
    let mut unknown = request.clone();
    unknown.model = "not-registered".into();
    let tools = request
        .clone()
        .with_tools(vec![smith::tool::ToolMetadata::new("read", "host tool")]);
    let thinking = request
        .clone()
        .with_params(smith::provider::ProviderParams {
            thinking: true,
            ..Default::default()
        });
    let oversized = request
        .clone()
        .with_params(smith::provider::ProviderParams {
            output_tokens_max: Some(17),
            ..Default::default()
        });
    for rejected in [unknown, tools, thinking, oversized] {
        let items = futures::executor::block_on(
            stream(rejected, smith::tool::CancelHandle::new()).collect::<Vec<_>>(),
        );
        assert_eq!(items.len(), 1);
        assert!(items[0].is_err());
    }
    for case in ["template", "origin", "binding", "artifact"] {
        let mut denied = cfg.clone();
        match case {
            "template" => denied.template_url = "https://unapproved.invalid/",
            "origin" => denied.endpoint = "http://unapproved.invalid".into(),
            "binding" => denied.protocol = ProtocolComponent::Anthropic,
            _ => denied.artifact = Some(artifact.clone()),
        }
        let selected = registered_protocol_transport(
            &registry,
            "example:custom/custom",
            "example:custom/fixture-model",
            "example:custom/fixture-key",
            denied,
        );
        match selected {
            Err(_) => {}
            Ok(selected) => {
                let items = futures::executor::block_on(
                    selected(request.clone(), smith::tool::CancelHandle::new()).collect::<Vec<_>>(),
                );
                assert_eq!(items.len(), 1);
                assert!(items[0].is_err());
            }
        }
    }
    let ungranted = PluginManifest::parse("id \"example:ungranted\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"fixture.wasm\"\ncapability \"net-scope\"\ncapability \"secret-proxy\"").unwrap();
    let error = PluginGeneration::load(&engine, ungranted, &artifact, &[Capability::NetScope])
        .err()
        .unwrap();
    assert!(error.to_string().contains("missing grant secret-proxy"));
    assert_eq!(
        drain.try_iter().count(),
        0,
        "rejected requests must not admit HTTP intent"
    );
    listener.set_nonblocking(true).unwrap();
    assert_eq!(
        listener.accept().unwrap_err().kind(),
        std::io::ErrorKind::WouldBlock
    );
    listener.set_nonblocking(false).unwrap();
    let (finished, completion) = std::sync::mpsc::sync_channel(1);
    // Selection remains pinned even when the registry itself is dropped.
    drop(registry);
    let server = std::thread::spawn(move || {
        let (mut socket, _) = listener.accept().unwrap();
        socket
            .set_write_timeout(Some(Duration::from_secs(10)))
            .unwrap();
        socket
            .set_read_timeout(Some(Duration::from_secs(10)))
            .unwrap();
        let mut bytes = Vec::new();
        let mut buffer = [0; 1024];
        while !bytes.ends_with(b"custom-wire-request") {
            let count = socket.read(&mut buffer).unwrap();
            assert!(count > 0);
            bytes.extend_from_slice(&buffer[..count]);
        }
        let request = String::from_utf8(bytes).unwrap();
        assert!(request.starts_with("POST /authorized HTTP/1.1"));
        assert!(
            request
                .to_ascii_lowercase()
                .contains("authorization: bearer isolated-secret")
        );
        let body = "custom codec: isolated-secret";
        write!(
            socket,
            "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
            body.len()
        )
        .unwrap();
        finished.send(()).unwrap();
    });
    let path = root.join("session.smh");
    let opened = open_session(&path, smith::config::Config::default_valid()).unwrap();
    let tools = ToolSession::with_session(&root, opened.session, opened.writer);
    let mut agent = Agent::with_provider_records(stream, tools, "fixture-model", Some(drain));
    let outcome = agent.run_turn("use registered codec").unwrap();
    assert_eq!(outcome.text, "custom codec: {{secret:connection-api-key}}");
    assert_eq!(outcome.reason, smith::stream::StopReason::EndTurn);
    assert!(!format!("{outcome:?}").contains("isolated-secret"));
    completion.recv_timeout(Duration::from_secs(15)).unwrap();
    server.join().unwrap();
    drop(agent);
    let reader = SessionReader::open(&path).unwrap();
    let mut provenances = Vec::new();
    let mut effects = 0;
    reader
        .scan(
            smith::config::Config::default_valid().frame_bytes_max,
            |frame| {
                if let smith_core::frame::Frame::Known { entry } = frame
                    && let smith_core::session::EntryContent::Meta { kind, detail } =
                        smith_core::session::EntryContent::decode(&entry.content)?
                {
                    if kind == smith_core::session::PROVIDER_DISPATCH_PROVENANCE_KIND {
                        provenances.push(serde_json::to_value(detail).unwrap());
                    }
                    if kind == smith_core::session::PROVIDER_HTTP_KIND {
                        effects += 1;
                    }
                }
                Ok(())
            },
        )
        .unwrap();
    assert_eq!(provenances.len(), 1);
    assert_eq!(provenances[0]["protocol_component_sha256"], hash);
    assert_eq!(
        provenances[0]["registration_binding"],
        serde_json::json!({
            "provider_id": "example:custom/custom", "model_id": "example:custom/fixture-model", "credential_source_id": "example:custom/fixture-key"
        })
    );
    assert_eq!(effects, 2);
    assert!(!String::from_utf8_lossy(&std::fs::read(&path).unwrap()).contains("isolated-secret"));
    std::fs::remove_dir_all(root).unwrap();
}

#[test]
fn invalid_protocol_candidate_is_rejected_before_publication() {
    let root = std::env::temp_dir().join(format!(
        "smith-invalid-protocol-candidate-{}",
        std::process::id()
    ));
    std::fs::create_dir_all(&root).unwrap();
    let artifact = root.join("invalid.wasm");
    std::fs::write(&artifact, b"invalid candidate").unwrap();
    let (records, _drain) = smith::provider::record_channel();
    let mut connection = config(&root, "http://127.0.0.1:1".into(), records);
    connection.protocol = ProtocolComponent::OpenAiCompatible;
    connection.artifact = Some(artifact);
    assert!(smith_harness::protocol::protocol_transport(connection).is_err());
    std::fs::remove_dir_all(root).unwrap();
}

#[cfg(unix)]
#[test]
fn registered_round_keeps_codec_and_whole_tool_batch_then_reloads_atomically() {
    coherent_round(true);
    coherent_round(false);
}

#[cfg(unix)]
fn coherent_round(registered: bool) {
    use smith_core::{
        agent::Agent,
        store::{SessionReader, open_session},
        tools::{ToolDispatcher, ToolSession},
    };
    use smith_harness::{
        plugin_control::ReloadControl, plugins::registry::PackageRegistry,
        wasm::runtime_tools_dispatcher,
    };
    use std::io::{Read, Write};
    let root = std::env::temp_dir().join(format!(
        "smith-coherent-round-{}-{registered}",
        std::process::id()
    ));
    std::fs::create_dir_all(&root).unwrap();
    let package = root.join("package");
    std::fs::create_dir_all(&package).unwrap();
    let source = include_str!("fixtures/coherent-provider.rs");
    let first = compile_fixture(&root.join("first"), source, "coherent-provider-fixture");
    let artifact = package.join("fixture.wasm");
    std::fs::copy(first, &artifact).unwrap();
    let first_hash = smith::sha256::digest_hex(&std::fs::read(&artifact).unwrap());
    let second = compile_fixture(
        &root.join("second"),
        &source.replace("\"v1\"", "\"v2\""),
        "coherent-provider-fixture",
    );
    let replacement = root.join("replacement.wasm");
    std::fs::copy(second, &replacement).unwrap();
    let second_hash = smith::sha256::digest_hex(&std::fs::read(&replacement).unwrap());
    let invalid = compile_fixture(
        &root.join("invalid"),
        &source.replace("\"v1\"", "\"v3\"").replace(
            "Ok(settings)",
            "Ok(ProviderSettings { include_usage: false, ..settings })",
        ),
        "coherent-provider-fixture",
    );
    let invalid_replacement = root.join("invalid-replacement.wasm");
    std::fs::copy(invalid, &invalid_replacement).unwrap();
    assert_ne!(first_hash, second_hash);
    std::fs::write(package.join("plugin.kdl"), "id \"example:coherent\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"fixture.wasm\"\ncapability \"net-scope\"\ncapability \"secret-proxy\"").unwrap();
    let engine = PluginEngine::new().unwrap();
    let packages = PackageRegistry::at(root.join("registry"));
    packages
        .link(
            &engine,
            &package,
            &[Capability::NetScope, Capability::SecretProxy],
        )
        .unwrap();
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(&root, std::fs::Permissions::from_mode(0o700)).unwrap();
    let control_path = root.join("control.sock");
    let control = ReloadControl::bind_at_path(control_path.clone()).unwrap();
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let endpoint = format!("http://{}", listener.local_addr().unwrap());
    let (records, drain) = smith::provider::record_channel();
    let mut connection = config(&root.join("auth"), endpoint, records);
    connection.catalog.models[0].capabilities.tool_use = true;
    let dispatcher =
        runtime_tools_dispatcher(&root, connection.auth_store.clone(), false, Some(control))
            .with_package_registry(packages);
    let (mut dispatcher, stream) = if registered {
        dispatcher.with_registered_protocol(
            connection,
            "example:coherent/custom".into(),
            "example:coherent/fixture-model".into(),
            "example:coherent/fixture-key".into(),
        )
    } else {
        connection.protocol = ProtocolComponent::OpenAiCompatible;
        dispatcher.with_protocol(connection)
    };
    dispatcher.begin_provider_round().unwrap();
    // Neither transport nor tools may rediscover this raw path before reload.
    std::fs::write(&artifact, b"swapped after publication").unwrap();

    let (sent, finished) = std::sync::mpsc::sync_channel(1);
    let server = std::thread::spawn(move || {
        for index in 0..3 {
            let (mut socket, _) = listener.accept().unwrap();
            socket
                .set_read_timeout(Some(Duration::from_secs(15)))
                .unwrap();
            socket
                .set_write_timeout(Some(Duration::from_secs(15)))
                .unwrap();
            let mut bytes = Vec::new();
            let mut buffer = [0; 4096];
            loop {
                let count = socket.read(&mut buffer).unwrap();
                assert!(count > 0);
                bytes.extend_from_slice(&buffer[..count]);
                if let Some(split) = bytes.windows(4).position(|part| part == b"\r\n\r\n") {
                    let header = String::from_utf8_lossy(&bytes[..split]);
                    let length = header
                        .lines()
                        .find_map(|line| {
                            let (key, value) = line.split_once(':')?;
                            key.eq_ignore_ascii_case("content-length")
                                .then(|| value.trim().parse::<usize>().unwrap())
                        })
                        .unwrap();
                    if bytes.len() >= split + 4 + length {
                        break;
                    }
                }
            }
            let wire = String::from_utf8(bytes).unwrap();
            if index == 0 {
                assert!(wire.contains("v1|v1-definition|"));
                std::fs::copy(&replacement, &artifact).unwrap();
            } else {
                assert!(wire.contains("v2|v2-definition|"), "{wire}");
                assert!(wire.contains("v1-handler"), "{wire}");
                if index == 1 {
                    // A single invalid replacement must retain codec + tool set.
                    std::fs::copy(&invalid_replacement, &artifact).unwrap();
                }
            }
            if index < 2 {
                let mut control = std::os::unix::net::UnixStream::connect(&control_path).unwrap();
                control
                    .set_read_timeout(Some(Duration::from_secs(5)))
                    .unwrap();
                control
                    .set_write_timeout(Some(Duration::from_secs(5)))
                    .unwrap();
                control.write_all(b"reload\n").unwrap();
                let mut reply = [0; 3];
                control.read_exact(&mut reply).unwrap();
                assert_eq!(&reply, b"ok\n");
            }
            let body = if index == 0 { "tool" } else { "done" };
            write!(
                socket,
                "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
                body.len()
            )
            .unwrap();
        }
        sent.send(()).unwrap();
    });
    let path = root.join("session.smh");
    let opened = open_session(&path, smith::config::Config::default_valid()).unwrap();
    let mut tools = ToolSession::with_session(&root, opened.session, opened.writer);
    tools.set_dispatcher(Box::new(dispatcher));
    let mut agent = Agent::with_provider_records(stream, tools, "fixture-model", Some(drain));
    assert_eq!(
        agent.run_turn("reload while streaming").unwrap().text,
        "v2:done"
    );
    assert_eq!(
        agent.run_turn("invalid replacement").unwrap().text,
        "v2:done"
    );
    finished.recv_timeout(Duration::from_secs(20)).unwrap();
    server.join().unwrap();
    drop(agent);
    let reader = SessionReader::open(&path).unwrap();
    let mut hashes = Vec::new();
    reader
        .scan(
            smith::config::Config::default_valid().frame_bytes_max,
            |frame| {
                if let smith_core::frame::Frame::Known { entry } = frame
                    && let smith_core::session::EntryContent::Meta { kind, detail } =
                        smith_core::session::EntryContent::decode(&entry.content)?
                    && kind == smith_core::session::PROVIDER_DISPATCH_PROVENANCE_KIND
                {
                    hashes.push(
                        serde_json::to_value(detail).unwrap()["protocol_component_sha256"]
                            .as_str()
                            .unwrap()
                            .to_string(),
                    );
                }
                Ok(())
            },
        )
        .unwrap();
    assert_eq!(hashes, [first_hash, second_hash.clone(), second_hash]);
    let durable = String::from_utf8_lossy(&std::fs::read(&path).unwrap()).into_owned();
    assert!(durable.contains("v1-handler"));
    assert!(!durable.contains("v2-handler"));
    std::fs::remove_dir_all(root).unwrap();
}

#[test]
fn round_worker_queues_intent_before_return_and_denial_never_starts_http() {
    use futures::StreamExt;
    use smith_core::tools::ToolDispatcher;
    let root = std::env::temp_dir().join(format!("smith-round-denied-{}", std::process::id()));
    std::fs::create_dir_all(&root).unwrap();
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let (records, drain) = smith::provider::record_channel();
    let mut connection = config(
        &root,
        format!("http://{}", listener.local_addr().unwrap()),
        records,
    );
    connection.protocol = ProtocolComponent::OpenAiCompatible;
    connection.template_url = "https://protocol.invalid/chat/completions";
    let dispatcher = smith_harness::wasm::runtime_tools_dispatcher(
        &root,
        connection.auth_store.clone(),
        false,
        None,
    )
    .with_package_registry(smith_harness::plugins::registry::PackageRegistry::at(
        root.join("registry"),
    ));
    let (mut dispatcher, stream) = dispatcher.with_protocol(connection);
    dispatcher.begin_provider_round().unwrap();
    // A preparation error has no end_provider_round callback. The next request
    // must still use a valid pin, not a stuck lease or abandoned generation.
    let bad = stream(
        smith::provider::ProviderRequest::new("missing", vec![]),
        smith::tool::CancelHandle::new(),
    );
    assert!(futures::executor::block_on(bad.collect::<Vec<_>>())[0].is_err());
    for _ in 0..2 {
        dispatcher.begin_provider_round().unwrap();
        let response = stream(
            smith::provider::ProviderRequest::new("fixture-model", vec![]),
            smith::tool::CancelHandle::new(),
        );
        let intents: Vec<_> = drain.try_iter().collect();
        assert_eq!(
            intents.len(),
            1,
            "initial intent must exist before stream consumption"
        );
        intents
            .into_iter()
            .next()
            .unwrap()
            .acknowledge(Err(smith::error::SmithError::Config {
                field: "fixture".into(),
                message: "denied durable admission".into(),
            }));
        let items = futures::executor::block_on(response.collect::<Vec<_>>());
        assert!(items.iter().any(Result::is_err));
        assert_eq!(
            drain.try_iter().count(),
            1,
            "denied exchange records its outcome"
        );
    }
    let cancel = smith::tool::CancelHandle::new();
    let response = stream(
        smith::provider::ProviderRequest::new("fixture-model", vec![]),
        cancel.clone(),
    );
    let intent = drain.try_iter().next().unwrap();
    cancel.cancel();
    intent.acknowledge(Ok(()));
    let items = futures::executor::block_on(response.collect::<Vec<_>>());
    assert!(items.iter().any(Result::is_err));
    assert_eq!(drain.try_iter().count(), 1);
    listener.set_nonblocking(true).unwrap();
    assert_eq!(
        listener.accept().unwrap_err().kind(),
        std::io::ErrorKind::WouldBlock
    );
    std::fs::remove_dir_all(root).unwrap();
}

#[cfg(unix)]
#[test]
fn nested_public_agent_pins_reload_and_stops_after_late_recording_crossing() {
    use futures::StreamExt;
    use smith_core::{
        agent::Agent,
        session::{EntryContent, EntryFrame},
        tools::{ToolDispatcher, ToolSession},
    };
    use smith_harness::{
        plugin_control::ReloadControl,
        plugins::registry::PackageRegistry,
        storage::StorageWorker,
        wasm::{CompositionGrants, ToolPolicy, runtime_tools_dispatcher},
    };
    use std::{
        io::{Read, Write},
        sync::{
            Arc,
            atomic::{AtomicUsize, Ordering},
        },
    };
    let root = std::env::temp_dir().join(format!(
        "smith-nested-reload-{}",
        smith::id::SessionId::new()
    ));
    std::fs::create_dir_all(root.join("A")).unwrap();
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(&root, std::fs::Permissions::from_mode(0o700)).unwrap();
    let source =
        include_str!("fixtures/composition.rs").replace("WIT_PATH", "wit/smith-plugin.wit");
    let first = compile_fixture(
        &root.join("first"),
        &source.replace("child committed", "v1 child committed"),
        "nested-reload-fixture",
    );
    let old = root.join("old.wasm");
    std::fs::copy(first, &old).unwrap();
    let second = compile_fixture(
        &root.join("second"),
        &source.replace("child committed", "v2 child committed"),
        "nested-reload-fixture",
    );
    let replacement = root.join("new.wasm");
    std::fs::copy(second, &replacement).unwrap();
    let old_hash = smith::sha256::digest_hex(&std::fs::read(&old).unwrap());
    let new_hash = smith::sha256::digest_hex(&std::fs::read(&replacement).unwrap());
    assert_ne!(old_hash, new_hash);
    let engine = PluginEngine::new().unwrap();
    let packages = PackageRegistry::at(root.join("registry"));
    for id in ["a", "target"] {
        let package = root.join(id);
        std::fs::create_dir(&package).unwrap();
        std::fs::copy(&old, package.join("fixture.wasm")).unwrap();
        std::fs::write(package.join("plugin.kdl"), format!("id \"fixture:{id}\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"fixture.wasm\"\ncapability \"workspace\"\n")).unwrap();
        packages
            .link(&engine, &package, &[Capability::Workspace])
            .unwrap();
    }
    let control_path = root.join("control.sock");
    let control = ReloadControl::bind_at_path(control_path.clone()).unwrap();
    let grants =
        CompositionGrants::from_specs(&["fixture:a/fixture=fixture:target/fixture#A".into()])
            .unwrap();
    let policy = ToolPolicy::from_specs(&["fixture:target/fixture=hidden".into()]).unwrap();
    let mut dispatcher = runtime_tools_dispatcher(
        &root,
        smith_ai::auth::AuthStore::at(root.join("auth")),
        false,
        Some(control),
    )
    .with_package_registry(packages)
    .with_composition_grants(grants)
    .with_tool_policy(policy);
    dispatcher.begin_provider_round().unwrap();
    dispatcher.end_provider_round();
    let artifact = root.join("target/fixture.wasm");
    std::fs::write(&artifact, b"invalid raw path after publication").unwrap();
    let (ready, received) = std::sync::mpsc::sync_channel(1);
    let (release, released) = std::sync::mpsc::sync_channel(1);
    let reloader = std::thread::spawn(move || {
        received.recv_timeout(Duration::from_secs(30)).unwrap();
        std::fs::copy(replacement, artifact).unwrap();
        let mut control = std::os::unix::net::UnixStream::connect(control_path).unwrap();
        control
            .set_read_timeout(Some(Duration::from_secs(5)))
            .unwrap();
        control
            .set_write_timeout(Some(Duration::from_secs(5)))
            .unwrap();
        control.write_all(b"reload\n").unwrap();
        let mut reply = [0; 3];
        control.read_exact(&mut reply).unwrap();
        assert_eq!(&reply, b"ok\n");
        release.send(()).unwrap();
    });
    let path = root.join("session.smh");
    let storage = Arc::new(StorageWorker::start(path.clone()));
    storage
        .open(smith::config::Config::default_valid())
        .unwrap();
    let receipt = storage
        .admit_foreground("compose", None)
        .unwrap()
        .work
        .receipt;
    let session = storage.claim(receipt.submission_id).unwrap();
    let appended = Arc::clone(&storage);
    let mut tools = ToolSession::with_persist(&root, session, move |entry, bytes| {
        appended.append(EntryFrame {
            id: entry.id,
            parent: entry.parent,
            timestamp_ms: entry.timestamp_ms,
            kind: entry.kind,
            content: bytes.to_vec(),
        })
    });
    let committed = Arc::clone(&storage);
    let mut signalled = false;
    tools = tools.with_completion_boundary(receipt.submission_id, move |frame| {
        let child = frame.entries().iter().any(|entry| {
            matches!(EntryContent::decode(&entry.content).unwrap(), EntryContent::Meta {kind,detail}
                if kind == smith_core::metadata::NESTED_INTENT_KIND && serde_json::to_value(&detail).unwrap()["operation"] == "fixture:target/fixture")
        });
        committed.commit_completion(frame)?;
        if child && !signalled {
            signalled = true;
            ready.send(()).unwrap();
            released.recv_timeout(Duration::from_secs(10)).unwrap();
        }
        Ok(())
    });
    tools.set_dispatcher(Box::new(dispatcher));
    let rounds = Arc::new(AtomicUsize::new(0));
    let requests = Arc::clone(&rounds);
    let workspace = root.clone();
    let stream: smith::provider::StreamFn = Arc::new(move |request, _| {
        let round = requests.fetch_add(1, Ordering::SeqCst);
        assert!(
            request
                .tools
                .iter()
                .any(|tool| tool.name == "fixture:a/fixture")
        );
        assert!(
            !request
                .tools
                .iter()
                .any(|tool| tool.name == "fixture:target/fixture")
        );
        if round > 0 {
            assert_eq!(
                std::fs::read_to_string(workspace.join("A/written.txt")).unwrap(),
                if round == 1 {
                    "v1 child committed"
                } else {
                    "v2 child committed"
                }
            );
        }
        let events = if round < 2 {
            vec![
                smith::stream::StreamEvent::tool_use(
                    smith::id::MessageId::new(),
                    "fixture:a/fixture",
                    smith::ToolArguments::from_serializable(&serde_json::json!({"mode":"caller"}))
                        .unwrap(),
                    smith::id::ToolCallId::new(),
                ),
                smith::stream::StreamEvent::stop(smith::stream::StopReason::ToolUse, None),
            ]
        } else {
            assert_eq!(round, 2);
            vec![
                smith::stream::StreamEvent::text_delta(smith::id::MessageId::new(), "done"),
                smith::stream::StreamEvent::stop(smith::stream::StopReason::EndTurn, None),
            ]
        };
        futures::stream::iter(events.into_iter().map(Ok)).boxed()
    });
    let mut agent = Agent::new(stream, tools, "fixture-model");
    assert_eq!(
        agent.run_turn("compose across reload").unwrap().text,
        "done"
    );
    assert_eq!(rounds.load(Ordering::SeqCst), 3);
    reloader.join().unwrap();
    drop(agent);
    drop(storage);
    let mut hashes = Vec::new();
    smith_core::store::SessionReader::open(&path)
        .unwrap()
        .scan(
            smith::config::Config::default_valid().frame_bytes_max,
            |frame| {
                for entry in frame.entries() {
                    if let EntryContent::Meta { kind, detail } =
                        EntryContent::decode(&entry.content)?
                        && kind == smith_core::metadata::NESTED_INTENT_KIND
                    {
                        let detail = serde_json::to_value(detail).unwrap();
                        if detail["operation"] == "fixture:target/fixture" {
                            hashes.push(
                                detail["component"]["component_sha256"]
                                    .as_str()
                                    .unwrap()
                                    .to_owned(),
                            );
                        }
                    }
                }
                Ok(())
            },
        )
        .unwrap();
    assert_eq!(hashes, [old_hash, new_hash]);
    composed_agent_stops_after_late_terminal_acknowledgement(&root);
    std::fs::remove_dir_all(root).unwrap();
}

struct LateAcknowledgement {
    dispatcher: Box<dyn smith_core::tools::ToolDispatcher>,
    usage: std::sync::Arc<std::sync::atomic::AtomicUsize>,
}
struct AcknowledgedUsage(std::sync::Arc<std::sync::atomic::AtomicUsize>);
impl smith_core::tools::InvocationAllocationObserver for AcknowledgedUsage {
    fn report(&self) -> smith::Result<smith_core::tools::InvocationAllocationReport> {
        let actual_bytes = self.0.load(std::sync::atomic::Ordering::SeqCst) as u64;
        Ok(smith_core::tools::InvocationAllocationReport {
            threshold_bytes: 100,
            actual_bytes,
            cleanup_bytes: 0,
            excess_bytes: actual_bytes.saturating_sub(100),
            allocations: 3,
            stopped_bytes: None,
        })
    }
}
impl smith_core::tools::ToolDispatcher for LateAcknowledgement {
    fn begin_provider_round(&mut self) -> smith::Result<smith_core::tools::ToolDispatcherSnapshot> {
        self.dispatcher.begin_provider_round()
    }
    fn end_provider_round(&mut self) {
        self.dispatcher.end_provider_round();
    }
    fn invoke(
        &mut self,
        name: &str,
        input: &smith::ToolArguments,
        effects: &mut smith_core::tools::ToolEffectContext<'_>,
    ) -> smith::Result<String> {
        let result = self.dispatcher.invoke(name, input, effects);
        // Inject only the observer threshold crossing, after real guest and
        // primitive execution; persistence acknowledgement advances it.
        assert!(
            effects
                .hold_allocation_observer(Box::new(AcknowledgedUsage(std::sync::Arc::clone(
                    &self.usage
                ))))
                .is_none()
        );
        effects.report_allocation(100, 50, 1);
        result
    }
}

fn composed_agent_stops_after_late_terminal_acknowledgement(root: &Path) {
    use futures::StreamExt;
    use smith_core::{
        agent::Agent,
        session::{EntryContent, EntryFrame},
        tools::ToolSession,
    };
    use smith_harness::{
        plugins::registry::PackageRegistry,
        storage::StorageWorker,
        wasm::{CompositionGrants, ToolPolicy, runtime_tools_dispatcher},
    };
    use std::sync::{
        Arc,
        atomic::{AtomicUsize, Ordering},
    };
    let workspace = root.join("late");
    std::fs::create_dir_all(workspace.join("A")).unwrap();
    let dispatcher = runtime_tools_dispatcher(
        &workspace,
        smith_ai::auth::AuthStore::at(root.join("auth")),
        false,
        None,
    )
    .with_package_registry(PackageRegistry::at(root.join("registry")))
    .with_composition_grants(
        CompositionGrants::from_specs(&["fixture:a/fixture=fixture:target/fixture#A".into()])
            .unwrap(),
    )
    .with_tool_policy(ToolPolicy::from_specs(&["fixture:target/fixture=hidden".into()]).unwrap());
    let path = workspace.join("session.smh");
    let storage = Arc::new(StorageWorker::start(path.clone()));
    storage
        .open(smith::config::Config::default_valid())
        .unwrap();
    let receipt = storage
        .admit_foreground("compose", None)
        .unwrap()
        .work
        .receipt;
    let session = storage.claim(receipt.submission_id).unwrap();
    let appended = Arc::clone(&storage);
    let mut tools = ToolSession::with_persist(&workspace, session, move |entry, bytes| {
        appended.append(EntryFrame {
            id: entry.id,
            parent: entry.parent,
            timestamp_ms: entry.timestamp_ms,
            kind: entry.kind,
            content: bytes.to_vec(),
        })
    });
    let committed = Arc::clone(&storage);
    let usage = Arc::new(AtomicUsize::new(50));
    let acknowledged = Arc::clone(&usage);
    tools = tools.with_completion_boundary(receipt.submission_id, move |frame| {
        let terminal = matches!(frame, smith_core::frame::Frame::Commit { .. });
        committed.commit_completion(frame)?;
        if terminal {
            acknowledged.store(125, Ordering::SeqCst);
        }
        Ok(())
    });
    tools.set_dispatcher(Box::new(LateAcknowledgement {
        dispatcher: Box::new(dispatcher),
        usage,
    }));
    let requests = Arc::new(AtomicUsize::new(0));
    let observed = Arc::clone(&requests);
    let calls = [smith::id::ToolCallId::new(), smith::id::ToolCallId::new()];
    let stream: smith::provider::StreamFn = Arc::new(move |_, _| {
        assert_eq!(
            observed.fetch_add(1, Ordering::SeqCst),
            0,
            "no provider request after terminal stop"
        );
        let message = smith::id::MessageId::new();
        let mut events = calls
            .map(|call| {
                smith::stream::StreamEvent::tool_use(
                    message,
                    "fixture:a/fixture",
                    smith::ToolArguments::from_serializable(&serde_json::json!({"mode":"caller"}))
                        .unwrap(),
                    call,
                )
            })
            .to_vec();
        events.push(smith::stream::StreamEvent::stop(
            smith::stream::StopReason::ToolUse,
            None,
        ));
        futures::stream::iter(events.into_iter().map(Ok)).boxed()
    });
    let mut agent = Agent::new(stream, tools, "fixture-model");
    let error = agent
        .run_turn("one completed effect then cleanup stop")
        .unwrap_err();
    assert!(error.to_string().contains("HostAllocation"));
    assert_eq!(requests.load(Ordering::SeqCst), 1);
    drop(agent);
    drop(storage);
    let mut stops = Vec::new();
    let mut completed = Vec::new();
    let mut children = 0;
    let mut following_effect = false;
    smith_core::store::SessionReader::open(&path)
        .unwrap()
        .scan(
            smith::config::Config::default_valid().frame_bytes_max,
            |frame| {
                for entry in frame.entries() {
                    match EntryContent::decode(&entry.content)? {
                        EntryContent::Meta { kind, detail }
                            if kind == smith_core::metadata::INVOCATION_TERMINAL_STOP_KIND =>
                        {
                            stops.push(
                                serde_json::from_value::<
                                    smith_core::metadata::InvocationTerminalStop,
                                >(
                                    serde_json::to_value(detail).unwrap()
                                )
                                .unwrap(),
                            )
                        }
                        EntryContent::Meta { kind, detail }
                            if kind == smith_core::metadata::NESTED_INTENT_KIND =>
                        {
                            if serde_json::to_value(detail).unwrap()["operation"]
                                == "fixture:target/fixture"
                            {
                                children += 1;
                            }
                        }
                        EntryContent::ToolResult { call_id, ok, .. } => {
                            completed.push((call_id, ok))
                        }
                        EntryContent::ToolCall { call_id, .. } if call_id == calls[1] => {
                            following_effect = true
                        }
                        _ => {}
                    }
                }
                Ok(())
            },
        )
        .unwrap();
    assert_eq!(completed, [(calls[0], true)]);
    assert_eq!(children, 1);
    assert!(!following_effect);
    assert_eq!(stops.len(), 1);
    let stop = &stops[0];
    assert_eq!(stop.call_id, calls[0]);
    assert_eq!(stop.submission_id, Some(receipt.submission_id));
    assert_eq!(
        stop.reason,
        smith_core::tools::InvocationStop::HostAllocation
    );
    assert_eq!(stop.allocation.actual_bytes, 125);
    assert_eq!(stop.allocation.cleanup_bytes, 75);
    assert_eq!(stop.allocation.excess_bytes, 25);
    assert_eq!(
        std::fs::read_to_string(workspace.join("A/written.txt")).unwrap(),
        "v2 child committed"
    );
}