Luigit
repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by admin

smith-harness/src/wasm/nested_tests.rs

Raw
//! SMH-SPEC-SPEC0001: admitted HTTP retains the advertised 1 MiB capability.
//! SMH-SPEC-PLUG0001 and SMH-SPEC-WASM0001: composed accounting, causal generation pins, sealed resources.
use super::*;
use crate::storage::StorageWorker;
use smith::tool::CancelHandle;
use smith_core::frame::Frame;
use smith_core::session::EntryContent;
use smith_core::store::SessionReader;
use smith_core::tools::{ToolDispatcher, ToolSession};
use std::io::{Read, Write};
use std::sync::Arc;
use std::sync::mpsc;
use std::time::Duration;

struct HttpDispatch {
    destination: String,
    bytes: usize,
    secret: Option<String>,
}
impl ToolDispatcher for HttpDispatch {
    fn invoke(
        &mut self,
        _: &str,
        _: &smith::structured::ToolArguments,
        effects: &mut ToolEffectContext<'_>,
    ) -> Result<String> {
        let engine = PluginEngine::new()?;
        let mut store = Store::new(
            engine.engine(),
            PluginHostState {
                composition: None,
                memory_bytes: 0,
                table_elements: 0,
                sealed_handles: 0,
                monotonic_now: 0,
                ceilings: PluginCeilings::default(),
                grants: NET_SCOPE_GRANT,
                effects_enabled: true,
                lifecycle_deadline: None,
                lifecycle_allocation_bytes: 0,
                plugin_id: "fixture:http".into(),
                net_scopes: vec![NetScope::parse(&self.destination).unwrap()],
                secret_grants: Vec::new(),
                oauth_authority: None,
                workspace_effects: WorkspaceEffects::default(),
                fatal_recording_failure: Cell::new(false),
                log_bytes: 0,
                log_entries: Vec::new(),
                secret_values: self.secret.iter().cloned().collect(),
                resolved_secret_refs: Vec::new(),
                mock_effects: None,
            },
        );
        let _attachment = attach_workspace_effects(&store, effects)?;
        let response = net_scope_request(
            store.data_mut(),
            bindings::smith::plugin::net_scope::HttpRequest {
                destination: self.destination.clone(),
                method: "POST".into(),
                headers: Vec::new(),
                body: vec![0; self.bytes],
                options: bindings::smith::plugin::net_scope::RequestOptions {
                    timeout_ms: 30_000,
                    response_bytes_max: NET_SCOPE_BYTES_MAX as u64,
                },
            },
        )
        .map_err(plugin_fault)?;
        assert_eq!(
            response.body,
            if self.secret.as_deref() == Some("\0") {
                b"<redacted>".repeat(self.bytes)
            } else {
                vec![0; self.bytes]
            }
        );
        Ok("complete".into())
    }
}

fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}

// The guest hex-encodes its input before returning it, bypassing downstream
// plaintext redactors: equality here proves the actual pre-decode boundary.
#[test]
fn authorized_protocol_guest_only_receives_sanitized_bytes_at_every_split() {
    use crate::protocol::*;
    use bindings::exports::smith::plugin::provider::{
        CatalogModel, CatalogSnapshot, CatalogSource, ModelCapabilities, ProviderSettings,
        SettingsProvenance,
    };
    struct Echo {
        secret: String,
        wire: Vec<u8>,
        split: usize,
        ending: Option<smith::http::HttpBodyEnd>,
    }
    impl smith::http::HttpExecutor for Echo {
        fn execute(
            &self,
            exchange: smith::http::HttpExchange,
            _: CancelHandle,
        ) -> Result<smith::http::HttpResponse> {
            assert_eq!(exchange.headers[0].value, format!("Bearer {}", self.secret));
            let (sender, body) = mpsc::sync_channel(3);
            for bytes in [&self.wire[..self.split], &self.wire[self.split..]] {
                sender
                    .send(smith::http::HttpBodyItem::Chunk(smith::http::HttpChunk {
                        bytes: bytes.to_vec(),
                    }))
                    .unwrap();
            }
            if let Some(ending) = &self.ending {
                sender
                    .send(smith::http::HttpBodyItem::End(ending.clone()))
                    .unwrap();
            }
            Ok(smith::http::HttpResponse {
                head: smith::http::HttpHead { status: 200 },
                body,
            })
        }
    }
    let dir = std::env::temp_dir().join(format!(
        "smith-response-boundary-{}",
        smith::id::SessionId::new()
    ));
    std::fs::create_dir(&dir).unwrap();
    let artifact = fixture_component(&dir);
    let engine = PluginEngine::new().unwrap();
    let manifest = PluginManifest::parse(
        "id \"fixture:codec\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"guest.wasm\"\n",
    )
    .unwrap();
    let mut generation = PluginGeneration::load(&engine, manifest, &artifact, &[]).unwrap();
    for (secret, spelling) in [
        ("a\"\\z", "a\"\\z"),
        ("a\"\\z", r#"\u0061\"\\\u007a"#),
        ("é😀/z", "é😀/z"),
        ("é😀/z", r#"é\uD83d\udE00\/\u007A"#),
        ("é😀/z", r#"\u00e9😀/z"#),
    ] {
        let suffix = secret.chars().next().unwrap();
        let wire = format!("before:{spelling}:after:{suffix}").into_bytes();
        for ending in [
            Some(smith::http::HttpBodyEnd::Complete),
            Some(smith::http::HttpBodyEnd::Cancelled),
            Some(smith::http::HttpBodyEnd::Truncated { limit: 10 }),
            Some(smith::http::HttpBodyEnd::Failed {
                message: "transport failed".into(),
            }),
            None,
        ] {
            for split in 0..=wire.len() {
                let (records, drain) = smith::provider::record_channel();
                let mut provider = ProtocolConnectionConfig {
                    connection_id: "test",
                    protocol: ProtocolComponent::OpenAiCompatible,
                    artifact: Some(artifact.clone()),
                    endpoint: "https://authorized.invalid".into(),
                    template_url: "https://protocol.invalid/chat/completions",
                    credential_header: "Authorization",
                    credential_prefix: "Bearer ",
                    static_headers: &[],
                    connection: ProtocolConnection::Api,
                    request_path: "/echo".into(),
                    credential: CredentialReference::explicit(secret).into(),
                    auth_store: AuthStore::at(dir.join("auth")),
                    executor: Arc::new(Echo {
                        secret: secret.into(),
                        wire: wire.clone(),
                        split,
                        ending: ending.clone(),
                    }),
                    records,
                    intent_admission_timeout: Duration::from_secs(10),
                    settings: ProviderSettings {
                        include_usage: true,
                        subscription: false,
                        provenance: SettingsProvenance::BuiltIn,
                    },
                    catalog: CatalogSnapshot {
                        generation: 1,
                        source: CatalogSource::Bundled,
                        models: vec![CatalogModel {
                            id: "test".into(),
                            capabilities: ModelCapabilities {
                                output_tokens_max: 1,
                                tool_use: false,
                                thinking: false,
                            },
                        }],
                    },
                };
                let exchange = authorize_template(
                    provider.template_url,
                    "Bearer {{secret:connection-api-key}}",
                    "{}".into(),
                    &provider,
                )
                .unwrap();
                let (admit, admitted) = mpsc::sync_channel(1);
                admit.send(Ok(())).unwrap();
                let (sender, received) = mpsc::sync_channel(64);
                let result = dispatch(
                    &provider,
                    "fixture-model",
                    exchange,
                    &crate::protocol::DispatchAdmission {
                        effect: admitted,
                        inference: None,
                    },
                    &CancelHandle::new(),
                    &mut generation,
                    &sender,
                );
                let complete = ending == Some(smith::http::HttpBodyEnd::Complete);
                assert_eq!(result.is_ok(), complete);
                drop(sender);
                let text: String = received
                    .into_iter()
                    .map(|event| match event.unwrap() {
                        smith::stream::StreamEvent::TextDelta { delta, .. } => delta,
                        _ => String::new(),
                    })
                    .collect();
                let expected = if complete {
                    format!("before:{{{{secret:connection-api-key}}}}:after:{suffix}")
                } else {
                    "before:{{secret:connection-api-key}}:after:".into()
                };
                assert_eq!(
                    text.replace('!', ""),
                    hex(expected.as_bytes()),
                    "split {split}"
                );
                let outcome = drain.try_iter().next().unwrap();
                let expected_outcome = if complete {
                    smith::provider::ProviderEffectOutcome::Completed
                } else if ending == Some(smith::http::HttpBodyEnd::Cancelled) {
                    smith::provider::ProviderEffectOutcome::Cancelled
                } else {
                    smith::provider::ProviderEffectOutcome::Failed
                };
                assert!(
                    matches!(outcome.record(), smith::provider::ProviderEffectRecord::Outcome { response_bytes, outcome, .. } if *response_bytes == wire.len() as u64 && *outcome == expected_outcome)
                );
                if complete && split == 0 {
                    provider.credential = CredentialReference::explicit("").into();
                    let unresolved = authorize_template(
                        provider.template_url,
                        "Bearer {{secret:connection-api-key}}",
                        "{}".into(),
                        &provider,
                    )
                    .unwrap();
                    let (admit, admitted) = mpsc::sync_channel(1);
                    admit
                        .send(Err(smith::error::SmithError::Cancelled))
                        .unwrap();
                    let (sender, _) = mpsc::sync_channel(1);
                    assert_eq!(
                        dispatch(
                            &provider,
                            "fixture-model",
                            unresolved,
                            &crate::protocol::DispatchAdmission {
                                effect: admitted,
                                inference: None
                            },
                            &CancelHandle::new(),
                            &mut generation,
                            &sender
                        ),
                        Err(smith::error::SmithError::Cancelled)
                    );
                }
            }
        }
    }
    std::fs::remove_dir_all(dir).unwrap();
}

struct ResponseDispatch {
    generation: PluginGeneration,
    input: String,
}
impl ToolDispatcher for ResponseDispatch {
    fn invoke(
        &mut self,
        _: &str,
        _: &smith::structured::ToolArguments,
        effects: &mut ToolEffectContext<'_>,
    ) -> Result<String> {
        self.generation
            .invoke_with_effects("fixture", &self.input, effects)
    }
}

#[test]
fn authorized_net_scope_sanitizes_guest_body_and_reopened_evidence_without_resolving_unused_grants()
{
    let dir = std::env::temp_dir().join(format!(
        "smith-net-boundary-{}",
        smith::id::SessionId::new()
    ));
    std::fs::create_dir(&dir).unwrap();
    let artifact = fixture_component(&dir);
    let engine = PluginEngine::new().unwrap();
    for (index, spelling) in ["a\"\\z", r#"\u0061\"\\\u007A"#].into_iter().enumerate() {
        for (complete, atomic) in [(true, true), (false, true)] {
            let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
            let destination = format!("http://{}", listener.local_addr().unwrap());
            let wire = format!("before:{spelling}:after:a");
            let (finished, completion) = mpsc::sync_channel(1);
            let server = std::thread::spawn(move || {
                let (mut socket, _) = listener.accept().unwrap();
                socket
                    .set_read_timeout(Some(Duration::from_secs(30)))
                    .unwrap();
                socket
                    .set_write_timeout(Some(Duration::from_secs(30)))
                    .unwrap();
                let mut header = Vec::new();
                while !header.ends_with(b"\r\n\r\n") {
                    let mut byte = [0];
                    socket.read_exact(&mut byte).unwrap();
                    header.push(byte[0]);
                    assert!(header.len() < 16384);
                }
                assert!(String::from_utf8(header).unwrap().contains("a\"\\z"));
                write!(
                    socket,
                    "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{wire}",
                    wire.len() + if complete { 0 } else { 8 }
                )
                .unwrap();
                finished.send(()).unwrap();
            });
            let manifest = PluginManifest::parse("id \"fixture:response\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"guest.wasm\"\ncapability \"net-scope\"\ncapability \"secret-proxy\"\n").unwrap();
            let mut generation = PluginGeneration::load(
                &engine,
                manifest,
                &artifact,
                &[Capability::NetScope, Capability::SecretProxy],
            )
            .unwrap();
            generation.configure_net_scopes(vec![NetScope::parse(&destination).unwrap()]);
            generation.configure_secret_grants(vec![SecretGrant {
                source_id: "connection-api-key".into(),
                origin: NetScope::parse(&destination).unwrap(),
                scopes: vec!["http-request".into()],
                credential: CredentialReference::explicit("a\"\\z"),
                auth_store: AuthStore::at(dir.join("auth")),
            }]);
            let path = dir.join(format!("{index}-{complete}-{atomic}.smh"));
            let (tools, writer) = admitted_tools(
                &path,
                Box::new(ResponseDispatch {
                    generation,
                    input: destination,
                }),
                atomic,
            );
            let writer = writer;
            let mut tools = tools;
            let output = tools.invoke("fixture", &smith::structured::ToolArguments::empty_object());
            completion.recv_timeout(Duration::from_secs(35)).unwrap();
            server.join().unwrap();
            if complete {
                let output = output.unwrap();
                assert!(output.ok);
                assert_eq!(output.output, hex(b"before:<redacted>:after:a"));
            } else {
                assert!(
                    output
                        .unwrap_err()
                        .to_string()
                        .contains("net-scope response failed")
                );
            }
            let expected = if complete {
                b"before:<redacted>:after:a".as_slice()
            } else {
                b"before:<redacted>:after:".as_slice()
            };
            let bytes = std::fs::read(&path).unwrap();
            assert!(!bytes.windows(4).any(|part| part == b"a\"\\z"));
            let reader = SessionReader::open(&path).unwrap();
            let limit = smith::config::Config::default_valid().frame_bytes_max;
            if atomic {
                let mut completions = Vec::new();
                reader
                    .scan(limit, |frame| {
                        for entry in frame.entries() {
                            if let EntryContent::Meta { kind, .. } =
                                EntryContent::decode(&entry.content)?
                                && kind == smith_core::metadata::NESTED_COMPLETION_KIND
                            {
                                completions.push(entry.id);
                            }
                        }
                        Ok(())
                    })
                    .unwrap();
                assert!(completions.len() >= 2);
                let evidence = reader
                    .nested_payload(*completions.last().unwrap(), limit)
                    .unwrap();
                assert!(evidence.contains(&hex(expected)), "{evidence}");
                assert!(!evidence.contains(&hex(spelling.as_bytes())));
                if !complete {
                    assert!(!evidence.contains(&hex(b"before:<redacted>:after:a")));
                }
            } else {
                let mut records = Vec::new();
                reader
                    .scan(limit, |frame| {
                        for entry in frame.entries() {
                            if let EntryContent::Meta { kind, detail } =
                                EntryContent::decode(&entry.content)?
                                && kind == smith_core::session::PLUGIN_HTTP_KIND
                            {
                                records.push(serde_json::to_value(detail).unwrap());
                            }
                        }
                        Ok(())
                    })
                    .unwrap();
                assert_eq!(records.len(), 2);
                assert_eq!(
                    records[1]["response_bytes"],
                    format!("before:{spelling}:after:a").len() as u64
                );
            }
            drop(tools);
            drop(writer);
        }
    }
    let manifest = PluginManifest::parse("id \"fixture:unused\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"guest.wasm\"\ncapability \"secret-proxy\"\n").unwrap();
    let mut generation =
        PluginGeneration::load(&engine, manifest, &artifact, &[Capability::SecretProxy]).unwrap();
    generation.configure_secret_grants(vec![SecretGrant {
        source_id: "unused".into(),
        origin: NetScope::parse("https://unused.invalid").unwrap(),
        scopes: vec!["http-request".into()],
        credential: CredentialReference::explicit(""),
        auth_store: AuthStore::at(dir.join("auth")),
    }]);
    let (tools, writer) = admitted_tools(
        &dir.join("unused.smh"),
        Box::new(ResponseDispatch {
            generation,
            input: String::new(),
        }),
        true,
    );
    let writer = writer;
    let mut tools = tools;
    assert!(
        tools
            .invoke("fixture", &smith::structured::ToolArguments::empty_object())
            .unwrap()
            .ok
    );
    drop(tools);
    drop(writer);
    std::fs::remove_dir_all(dir).unwrap();
}

pub(super) fn admitted_tools(
    path: &std::path::Path,
    dispatcher: Box<dyn ToolDispatcher>,
    atomic: bool,
) -> (ToolSession, Arc<StorageWorker>) {
    let storage = Arc::new(StorageWorker::start(path.to_path_buf()));
    let session = storage
        .open(smith::config::Config::default_valid())
        .unwrap();
    let receipt = atomic.then(|| {
        storage
            .admit_foreground("invoke", None)
            .unwrap()
            .work
            .receipt
    });
    let session = receipt
        .as_ref()
        .map_or(Ok(session), |receipt| storage.claim(receipt.submission_id))
        .unwrap();
    let appended = Arc::clone(&storage);
    let committed = Arc::clone(&storage);
    let mut tools =
        ToolSession::with_persist(path.parent().unwrap(), session, move |entry, bytes| {
            appended.append(smith_core::session::EntryFrame {
                id: entry.id,
                parent: entry.parent,
                timestamp_ms: entry.timestamp_ms,
                kind: entry.kind,
                content: bytes.to_vec(),
            })
        });
    if let Some(receipt) = receipt {
        tools = tools.with_completion_boundary(receipt.submission_id, move |frame| {
            committed.commit_completion(frame)
        });
    }
    tools.register(smith::tool::ToolMetadata::new("fixture", "nested fixture"));
    tools.set_dispatcher(dispatcher);
    (tools, storage)
}

struct ComponentDispatch(PluginGeneration);
impl ToolDispatcher for ComponentDispatch {
    fn invoke(
        &mut self,
        _: &str,
        _: &smith::structured::ToolArguments,
        effects: &mut ToolEffectContext<'_>,
    ) -> Result<String> {
        self.0.invoke_with_effects("fixture", "{}", effects)
    }
}

fn fixture_component(dir: &std::path::Path) -> std::path::PathBuf {
    fixture_component_source(dir, include_str!("../../tests/fixtures/nested-effects.rs"))
}

pub(super) fn fixture_component_source(dir: &std::path::Path, source: &str) -> std::path::PathBuf {
    let project = dir.join("guest");
    std::fs::create_dir(&project).unwrap();
    let workspace = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap();
    let dependency = include_str!("../../../Cargo.toml")
        .lines()
        .find(|line| line.starts_with("wit-bindgen ="))
        .unwrap();
    std::fs::write(project.join("Cargo.toml"), format!("[workspace]\n[package]\nname = \"nested-effects-fixture\"\nversion = \"0.1.0\"\nedition = \"2024\"\n[lib]\npath = \"lib.rs\"\ncrate-type = [\"cdylib\"]\n[dependencies]\n{dependency}\n")).unwrap();
    std::fs::write(
        project.join("build.rs"),
        include_str!("../../../plugins/coding-tools/build.rs"),
    )
    .unwrap();
    std::fs::write(
        project.join("lib.rs"),
        source.replace(
            "WIT_PATH",
            workspace.join("wit/smith-plugin.wit").to_str().unwrap(),
        ),
    )
    .unwrap();
    for arguments in [
        vec!["generate-lockfile", "--offline"],
        vec![
            "build",
            "--locked",
            "--offline",
            "--target",
            "wasm32-wasip2",
        ],
    ] {
        let mut command = std::process::Command::new("cargo");
        let cargo_home = std::env::var_os("CARGO_HOME").map_or_else(
            || PathBuf::from(std::env::var_os("HOME").unwrap()).join(".cargo"),
            PathBuf::from,
        );
        command
            .current_dir(&project)
            .args(arguments)
            .env_remove("CARGO_ENCODED_RUSTFLAGS")
            .env("RUSTFLAGS", "-C target-cpu=generic")
            .env("CARGO_HOME", cargo_home)
            .env("HOME", project.join("home"))
            .env("XDG_CONFIG_HOME", project.join("config"))
            .env("XDG_DATA_HOME", project.join("data"))
            .env("XDG_STATE_HOME", project.join("state"))
            .env("XDG_CACHE_HOME", project.join("cache"))
            .env_remove("OPENAI_API_KEY")
            .env_remove("ANTHROPIC_API_KEY")
            .env_remove("GOOGLE_API_KEY")
            .env_remove("GEMINI_API_KEY")
            .env_remove("ZAI_API_KEY")
            .env_remove("SMITH_PLUGIN_API_KEY");
        let (done, received) = std::sync::mpsc::sync_channel(1);
        let worker = std::thread::spawn(move || done.send(command.output().unwrap()).unwrap());
        let output = received.recv_timeout(Duration::from_secs(180)).unwrap();
        worker.join().unwrap();
        assert!(
            output.status.success(),
            "{}",
            String::from_utf8_lossy(&output.stderr)
        );
    }
    let artifact = dir.join("nested-effects-fixture.wasm");
    std::fs::copy(
        project.join("target/wasm32-wasip2/debug/nested_effects_fixture.wasm"),
        &artifact,
    )
    .unwrap();
    std::fs::remove_dir_all(project.join("target")).unwrap();
    artifact
}

#[test]
fn real_guest_cannot_start_write_two_before_write_one_completion_acknowledgement() {
    let dir = std::env::temp_dir().join(format!("smith-guest-ack-{}", smith::id::SessionId::new()));
    std::fs::create_dir(&dir).unwrap();
    let artifact = fixture_component(&dir);
    let engine = PluginEngine::new().unwrap();
    for fail in [false, true] {
        let run = dir.join(if fail { "failure" } else { "success" });
        std::fs::create_dir(&run).unwrap();
        let manifest = PluginManifest::parse("id \"fixture:two-writes\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"guest.wasm\"\ncapability \"workspace\"\n").unwrap();
        let generation =
            PluginGeneration::load(&engine, manifest, &artifact, &[Capability::Workspace]).unwrap();
        let path = run.join("session.smh");
        let (tools, writer) = admitted_tools(&path, Box::new(ComponentDispatch(generation)), true);
        let reader = SessionReader::open(&path).unwrap();
        let mut submission = None;
        reader
            .scan(
                smith::config::Config::default_valid().frame_bytes_max,
                |frame| {
                    if let Frame::Claim { submission_id } = frame {
                        submission = Some(submission_id);
                    }
                    Ok(())
                },
            )
            .unwrap();
        drop(reader);
        let committed = Arc::clone(&writer);
        let callback_run = run.clone();
        let mut completions = 0;
        let mut tools = tools.with_completion_boundary(submission.unwrap(), move |frame| {
            if matches!(frame, Frame::NestedCommit { .. }) {
                completions += 1;
                if completions == 1 {
                    assert_eq!(
                        std::fs::read_to_string(callback_run.join("first.txt")).unwrap(),
                        "once"
                    );
                    assert!(
                        !callback_run.join("second.txt").exists(),
                        "guest resumed before completion ACK"
                    );
                    if fail {
                        return Err(smith::SmithError::Cancelled);
                    }
                }
            } else {
                assert!(
                    completions == 2,
                    "outer/provider continuation before both nested ACKs"
                );
            }
            committed.commit_completion(frame)
        });
        let result = tools.invoke("fixture", &smith::structured::ToolArguments::empty_object());
        assert_eq!(result.is_err(), fail);
        assert_eq!(run.join("second.txt").exists(), !fail);
        if fail {
            assert!(
                tools
                    .invoke("fixture", &smith::structured::ToolArguments::empty_object())
                    .is_err()
            );
        }
        drop(tools);
        drop(writer);
    }
    std::fs::remove_dir_all(dir).unwrap();
}

#[test]
fn admitted_http_declaring_one_mib_accepts_large_body_and_complete_redacted_journal() {
    for (bytes, secret) in [
        (80 * 1024, Some("0".to_string())),
        (80 * 1024, None),
        (NET_SCOPE_BYTES_MAX, None),
        (NET_SCOPE_BYTES_MAX, Some("\0".to_string())),
    ] {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let destination = format!("http://{}", listener.local_addr().unwrap());
        let (done, received) = std::sync::mpsc::sync_channel(1);
        let server = std::thread::spawn(move || {
            let (mut stream, _) = listener.accept().unwrap();
            stream
                .set_read_timeout(Some(Duration::from_secs(120)))
                .unwrap();
            stream
                .set_write_timeout(Some(Duration::from_secs(120)))
                .unwrap();
            let mut header = Vec::new();
            while !header.ends_with(b"\r\n\r\n") {
                let mut byte = [0];
                stream.read_exact(&mut byte).unwrap();
                header.push(byte[0]);
                assert!(header.len() <= 16 * 1024);
            }
            let mut body = vec![0; bytes];
            stream.read_exact(&mut body).unwrap();
            assert_eq!(body, vec![0; bytes]);
            write!(
                stream,
                "HTTP/1.1 200 OK\r\nContent-Length: {bytes}\r\nConnection: close\r\n\r\n"
            )
            .unwrap();
            stream.write_all(&body).unwrap();
            done.send(()).unwrap();
        });
        let dir = std::env::temp_dir().join(format!(
            "smith-http-payload-{}",
            smith::id::SessionId::new()
        ));
        std::fs::create_dir(&dir).unwrap();
        let path = dir.join("session.smh");
        let (tools, writer) = admitted_tools(
            &path,
            Box::new(HttpDispatch {
                destination,
                bytes,
                secret: secret.clone(),
            }),
            true,
        );
        let writer = writer;
        let mut tools = tools;
        assert!(
            tools
                .invoke("fixture", &smith::structured::ToolArguments::empty_object())
                .unwrap()
                .ok
        );
        received.recv_timeout(Duration::from_secs(180)).unwrap();
        server.join().unwrap();
        let reader = SessionReader::open(&path).unwrap();
        let limit = smith::config::Config::default_valid().frame_bytes_max;
        reader.verify_full(limit).unwrap();
        let mut markers = Vec::new();
        let mut outer = 0;
        reader
            .scan(limit, |frame| {
                if matches!(frame, Frame::Commit { .. }) {
                    outer += 1;
                }
                for entry in frame.entries() {
                    if let EntryContent::Meta { kind, .. } = EntryContent::decode(&entry.content)?
                        && (kind == smith_core::metadata::NESTED_INTENT_KIND
                            || kind == smith_core::metadata::NESTED_COMPLETION_KIND)
                    {
                        markers.push(entry.id);
                    }
                }
                Ok(())
            })
            .unwrap();
        assert_eq!(outer, 1);
        assert_eq!(markers.len(), 2);
        for marker in markers {
            let evidence: serde_json::Value =
                serde_json::from_str(&reader.nested_payload(marker, limit).unwrap()).unwrap();
            let body = evidence["body_hex"].as_str().unwrap();
            assert_eq!(
                body,
                if secret.as_deref() == Some("\0") {
                    "3c72656461637465643e".repeat(bytes)
                } else {
                    "00".repeat(bytes)
                }
            );
        }
        drop(reader);
        drop(tools);
        drop(writer);
        std::fs::remove_dir_all(dir).unwrap();
    }
}

#[test]
fn source_declaring_codec_keeps_its_advertised_and_executable_tool() {
    let dir =
        std::env::temp_dir().join(format!("smith-codec-tools-{}", smith::id::SessionId::new()));
    std::fs::create_dir(&dir).unwrap();
    let artifact = fixture_component(&dir);
    let package = dir.join("package");
    std::fs::create_dir(&package).unwrap();
    std::fs::copy(artifact, package.join("guest.wasm")).unwrap();
    std::fs::write(package.join("plugin.kdl"), "id \"fixture:codec-tools\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"guest.wasm\"\ncapability \"workspace\"\ncapability \"net-scope\"\ncapability \"secret-proxy\"\n").unwrap();
    let packages = PackageRegistry::at(dir.join("registry"));
    packages
        .link(
            &PluginEngine::new().unwrap(),
            &package,
            &[
                Capability::Workspace,
                Capability::NetScope,
                Capability::SecretProxy,
            ],
        )
        .unwrap();
    let mut dispatcher =
        runtime_tools_dispatcher(&dir, AuthStore::at(dir.join("auth")), false, None);
    dispatcher.packages = packages;
    let snapshot = dispatcher.begin_provider_round().unwrap();
    assert_eq!(snapshot.tools.len(), 1);
    assert_eq!(snapshot.tools[0].name, "fixture:codec-tools/fixture");
    assert_eq!(
        snapshot.provenance.components[0].source_id,
        "fixture:codec-tools"
    );
    let (mut tools, writer) = admitted_tools(&dir.join("session.smh"), Box::new(dispatcher), true);
    tools.begin_provider_round().unwrap();
    assert!(
        tools
            .invoke(
                "fixture:codec-tools/fixture",
                &ToolArguments::empty_object()
            )
            .unwrap()
            .ok
    );
    assert_eq!(
        std::fs::read_to_string(dir.join("first.txt")).unwrap(),
        "once"
    );
    assert_eq!(
        std::fs::read_to_string(dir.join("second.txt")).unwrap(),
        "twice"
    );
    drop(tools);
    drop(writer);
    std::fs::remove_dir_all(dir).unwrap();
}

struct HostComposition;
impl ToolDispatcher for HostComposition {
    fn invoke(
        &mut self,
        _: &str,
        _: &smith::structured::ToolArguments,
        effects: &mut ToolEffectContext<'_>,
    ) -> Result<String> {
        effects.with_child("compose", "outer child input", None, |child| {
            child.with_child("write", &"i".repeat(65_537), None, |grandchild| {
                let arguments = smith::structured::ToolArguments::from_serializable(
                    &serde_json::json!({"path":"composed.txt","content":"survives"}),
                )
                .unwrap();
                grandchild.invoke("write", &arguments)?;
                Ok("o".repeat(65_537))
            })
        })?;
        Err(smith::SmithError::Cancelled)
    }
}

#[test]
fn host_composition_worker_preserves_two_level_effects_and_chunk_ownership() {
    let directory = std::env::temp_dir().join(format!(
        "smith-worker-causal-{}",
        smith::id::SessionId::new()
    ));
    std::fs::create_dir(&directory).unwrap();
    let path = directory.join("session.smh");
    let (mut tools, worker) = admitted_tools(&path, Box::new(HostComposition), true);
    assert!(matches!(
        tools.invoke("fixture", &smith::structured::ToolArguments::empty_object()),
        Err(smith::SmithError::Cancelled)
    ));
    assert_eq!(
        std::fs::read_to_string(directory.join("composed.txt")).unwrap(),
        "survives"
    );
    drop(tools);
    drop(worker);
    let reader = SessionReader::open(&path).unwrap();
    let limit = smith::config::Config::default_valid().frame_bytes_max;
    reader.validate_work(limit).unwrap();
    let mut records = Vec::new();
    reader
        .scan(limit, |frame| {
            if let Frame::Known { entry } | Frame::NestedCommit { entry } = frame
                && let EntryContent::Meta { kind, detail } = EntryContent::decode(&entry.content)?
                && matches!(
                    kind.as_str(),
                    smith_core::metadata::NESTED_INTENT_KIND
                        | smith_core::metadata::NESTED_COMPLETION_KIND
                )
            {
                records.push((
                    entry.id,
                    entry.parent,
                    serde_json::to_value(detail).unwrap(),
                ));
            }
            Ok(())
        })
        .unwrap();
    assert_eq!(records.len(), 6);
    let root_call: smith::id::ToolCallId =
        serde_json::from_value(records[0].2["call_id"].clone()).unwrap();
    let session = reader.selected_session(limit).unwrap();
    assert_eq!(
        session.mutation_state(root_call).unwrap(),
        smith::error::MutationState::Applied
    );
    assert_eq!(
        session
            .active_branch()
            .entries()
            .iter()
            .filter(|entry| matches!(entry.kind, smith_core::session::EntryKind::ToolCall { .. }))
            .count(),
        1
    );
    assert!(
        records
            .iter()
            .all(|entry| entry.2["call_id"] == records[0].2["call_id"]
                && entry.2["submission_id"] == records[0].2["submission_id"])
    );

    assert_eq!(
        records[1].2["causal"]["parent"],
        records[0].2["causal"]["effect_id"]
    );
    assert_eq!(
        records[2].2["causal"]["parent"],
        records[1].2["causal"]["effect_id"]
    );
    assert_eq!(records[3].2["causal"], records[2].2["causal"]);
    assert_eq!(records[4].2["causal"], records[1].2["causal"]);
    assert_eq!(records[5].2["causal"], records[0].2["causal"]);
    assert_eq!(
        records
            .iter()
            .map(|entry| entry.2["index"].as_u64().unwrap())
            .collect::<Vec<_>>(),
        [0, 1, 2, 2, 1, 0]
    );
    assert_eq!(
        reader.nested_payload(records[1].0, limit).unwrap(),
        "i".repeat(65_537)
    );
    assert_eq!(
        reader.nested_payload(records[4].0, limit).unwrap(),
        "o".repeat(65_537)
    );
    assert_eq!(
        reader.nested_payload(records[5].0, limit).unwrap(),
        "o".repeat(65_537)
    );
    let mut trace = Vec::new();
    smith_core::trace::replay(&reader, limit, |record| {
        trace.push(record);
        Ok(())
    })
    .unwrap();
    let causal = trace
        .iter()
        .filter(|record| {
            matches!(
                record.kind,
                smith_core::trace::TraceKind::ChildInvocationStarted
                    | smith_core::trace::TraceKind::ChildInvocationCompleted
                    | smith_core::trace::TraceKind::NestedEffectStarted
                    | smith_core::trace::TraceKind::NestedEffectCompleted
            )
        })
        .collect::<Vec<_>>();
    assert_eq!(causal.len(), 6);
    assert!(
        causal[4]
            .event_ref
            .contains(&format!("effect:{}", records[1].0))
    );
    assert!(
        causal[5]
            .event_ref
            .contains(&format!("effect:{}", records[0].0))
    );
    drop(reader);
    std::fs::remove_dir_all(directory).unwrap();
}

#[test]
fn composition_two_consumers_enter_real_hidden_target() {
    struct Dispatch {
        registry: PluginRegistry,
        caller: String,
        input: String,
        grants: CompositionGrants,
        policy: ToolPolicy,
        expected_stop: Option<smith_core::tools::InvocationStop>,
    }
    impl ToolDispatcher for Dispatch {
        fn invoke(
            &mut self,
            _: &str,
            _: &ToolArguments,
            effects: &mut ToolEffectContext<'_>,
        ) -> Result<String> {
            let result = self.registry.invoke_composed(
                &self.caller,
                &self.input,
                effects,
                &self.grants,
                &self.policy,
            );
            if let Some(expected) = self.expected_stop {
                assert_eq!(
                    effects.stop_reason(),
                    Some(expected),
                    "{}: {result:?}; {:?}",
                    self.input,
                    effects.allocation_report()
                );
                if self.input.contains("call-count") {
                    let report = effects.allocation_report().unwrap();
                    assert!(
                        report.actual_bytes <= report.threshold_bytes,
                        "{} intended count boundary must precede host allocation: {report:?}",
                        self.input
                    );
                }
                if expected == smith_core::tools::InvocationStop::HostAllocation
                    && self.input.contains("wasi-host")
                {
                    let report = effects.allocation_report().unwrap();
                    assert!(report.actual_bytes > report.threshold_bytes, "{report:?}");
                    assert_eq!(
                        report.excess_bytes,
                        report.actual_bytes - report.threshold_bytes
                    );
                    assert!(report.stopped_bytes.unwrap() <= report.actual_bytes);
                }
            }
            assert_eq!(
                self.registry.generations.len(),
                3,
                "pool must restore every executable after failure"
            );
            result
        }
    }
    let dir =
        std::env::temp_dir().join(format!("smith-composition-{}", smith::id::SessionId::new()));
    std::fs::create_dir(&dir).unwrap();
    let artifact =
        fixture_component_source(&dir, include_str!("../../tests/fixtures/composition.rs"));
    let engine = PluginEngine::new().unwrap();
    for (index, (caller, mode, granted, disabled, success, written)) in [
        ("a", "caller", true, false, true, true),
        ("b", "caller", true, false, true, true),
        ("a", "catch", false, false, false, false),
        ("a", "catch", true, true, false, false),
        ("a", "escape-catch", true, false, false, false),
        ("a", "invalid-catch", true, false, true, false),
        ("a", "ordinary-catch", true, false, true, false),
        ("a", "parent-fail", true, false, false, true),
        ("a", "reentry-catch", true, false, false, false),
        ("a", "hop", true, false, false, false),
        ("a", "fanout-catch", true, false, false, true),
        ("a", "overflow", true, false, false, false),
        ("a", "call-count", true, false, false, false),
        ("a", "conversion-budget", true, false, false, false),
        ("a", "output-budget", true, false, false, false),
        ("a", "memory-budget", true, false, false, false),
        ("a", "table-budget", true, false, false, false),
        ("a", "depth-budget", true, false, false, false),
        ("a", "wasi-host", true, false, false, true),
        ("a", "external-cancel", true, false, false, false),
        ("a", "spin-fuel", true, false, false, false),
        ("a", "spin-deadline", true, false, false, false),
        ("a", "recording-fail-catch", true, false, false, true),
    ]
    .into_iter()
    .enumerate()
    {
        let run = dir.join(index.to_string());
        std::fs::create_dir_all(run.join("A")).unwrap();
        std::fs::create_dir(run.join("B")).unwrap();
        let mut registry = PluginRegistry::new();
        let generations = ["a", "b", "target"].map(|id| {
            let manifest = PluginManifest::parse(&format!("id \"fixture:{id}\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"guest.wasm\"\ncapability \"workspace\"\n")).unwrap();
            PluginGeneration::load(&engine, manifest, &artifact, &[Capability::Workspace]).unwrap()
        });
        registry.publish_all(generations.into()).unwrap();
        let caller = format!("fixture:{caller}/fixture");
        let mut grants = CompositionGrants::from_specs(&if granted {
            vec![
                format!("{caller}=fixture:target/fixture#A"),
                "fixture:a/fixture=fixture:a/fixture#A".into(),
                "fixture:a/fixture=fixture:b/fixture#A".into(),
                "fixture:b/fixture=fixture:a/fixture#.".into(),
            ]
            .into_iter()
            .collect::<std::collections::BTreeSet<_>>()
            .into_iter()
            .collect::<Vec<_>>()
        } else {
            Vec::new()
        })
        .unwrap();
        let expected_stop = match mode {
            "call-count" => Some(smith_core::tools::InvocationStop::HostAllocation),
            "conversion-budget" => {
                grants = grants
                    .with_resource_limits(CompositionResourceLimits {
                        memory_bytes: 64 * 1024 * 1024,
                        table_elements: 10_000,
                        conversion_bytes: 1,
                        output_bytes: 1024 * 1024,
                        component_handles: 1024,
                        calls: 32,
                        depth: 8,
                    })
                    .unwrap();
                Some(smith_core::tools::InvocationStop::Conversion)
            }
            "output-budget" => {
                grants = grants
                    .with_resource_limits(CompositionResourceLimits {
                        memory_bytes: 64 * 1024 * 1024,
                        table_elements: 10_000,
                        conversion_bytes: 8 * 1024 * 1024,
                        output_bytes: 1,
                        component_handles: 1024,
                        calls: 32,
                        depth: 8,
                    })
                    .unwrap();
                Some(smith_core::tools::InvocationStop::Output)
            }
            "memory-budget" => {
                grants = grants
                    .with_resource_limits(CompositionResourceLimits {
                        memory_bytes: 1,
                        table_elements: 10_000,
                        conversion_bytes: 8 * 1024 * 1024,
                        output_bytes: 1024 * 1024,
                        component_handles: 1024,
                        calls: 32,
                        depth: 8,
                    })
                    .unwrap();
                Some(smith_core::tools::InvocationStop::Memory)
            }
            "table-budget" => {
                grants = grants
                    .with_resource_limits(CompositionResourceLimits {
                        memory_bytes: 64 * 1024 * 1024,
                        table_elements: 1,
                        conversion_bytes: 8 * 1024 * 1024,
                        output_bytes: 1024 * 1024,
                        component_handles: 1024,
                        calls: 32,
                        depth: 8,
                    })
                    .unwrap();
                Some(smith_core::tools::InvocationStop::Table)
            }
            "depth-budget" => {
                grants = grants
                    .with_resource_limits(CompositionResourceLimits {
                        memory_bytes: 64 * 1024 * 1024,
                        table_elements: 10_000,
                        conversion_bytes: 8 * 1024 * 1024,
                        output_bytes: 1024 * 1024,
                        component_handles: 1024,
                        calls: 32,
                        depth: 1,
                    })
                    .unwrap();
                Some(smith_core::tools::InvocationStop::HostAllocation)
            }
            "external-cancel" => Some(smith_core::tools::InvocationStop::Cancelled),
            "wasi-host" => {
                grants = grants.with_host_allocation_limit(2 * 1024 * 1024).unwrap();
                Some(smith_core::tools::InvocationStop::HostAllocation)
            }
            "spin-fuel" => {
                grants = grants
                    .with_execution_limits(10_000, Duration::from_secs(30))
                    .unwrap();
                Some(smith_core::tools::InvocationStop::Fuel)
            }
            "spin-deadline" => {
                grants = grants
                    .with_execution_limits(10_000_000_000, Duration::from_millis(100))
                    .unwrap();
                Some(smith_core::tools::InvocationStop::Deadline)
            }
            _ => None,
        };
        let policy = ToolPolicy::from_specs(&[format!(
            "fixture:target/fixture={}",
            if disabled { "disabled" } else { "hidden" }
        )])
        .unwrap();
        let dispatcher = Dispatch {
            registry,
            caller,
            input: format!(r#"{{"mode":"{mode}"}}"#),
            grants,
            policy,
            expected_stop,
        };
        let path = run.join("session.smh");
        let (mut tools, writer) = admitted_tools(&path, Box::new(dispatcher), true);
        let mut cancellation_worker = None;
        if mode == "external-cancel" {
            let (ready, receiver) = std::sync::mpsc::sync_channel(1);
            super::composition::signal_guest_epoch(ready);
            let cancel = tools.cancel_handle();
            cancellation_worker = Some(std::thread::spawn(move || {
                receiver.recv_timeout(Duration::from_secs(5)).unwrap();
                cancel.cancel();
            }));
        }
        if matches!(mode, "recording-fail-catch" | "call-count") {
            let mut submission = None;
            SessionReader::open(&path)
                .unwrap()
                .scan(
                    smith::config::Config::default_valid().frame_bytes_max,
                    |frame| {
                        if let Frame::Claim { submission_id } = frame {
                            submission = Some(submission_id);
                        }
                        Ok(())
                    },
                )
                .unwrap();
            let committed = Arc::clone(&writer);
            tools = tools.with_completion_boundary(submission.unwrap(), move |frame| {
                // Isolate the call-count boundary from the storage worker's
                // independently tested cumulative allocation threshold.
                // The real component and core ledger still run; this case
                // deliberately uses an in-memory persistence acknowledgement.
                if mode == "call-count" {
                    return Ok(());
                }

                let nested = matches!(frame, Frame::NestedCommit { .. });
                if nested && mode == "recording-fail-catch" {
                    return Err(smith::SmithError::Cancelled);
                }
                committed.commit_completion(frame)?;
                Ok(())
            });
        }
        let result = tools.invoke("fixture", &ToolArguments::empty_object());
        if let Some(worker) = cancellation_worker {
            worker.join().unwrap();
        }
        assert_eq!(
            result.as_ref().is_ok_and(|result| result.ok),
            success,
            "{mode}: {result:?}"
        );
        if mode == "call-count" {
            let intents = tools.session.active_branch().entries().iter().filter(|entry| {
                matches!(tools.session.content(entry).unwrap(), EntryContent::Meta {kind,..} if kind == smith_core::metadata::NESTED_INTENT_KIND)
            }).count();
            assert_eq!(
                intents, 31,
                "root plus 31 children exhaust 32 calls before child 32"
            );
        }
        if success {
            let report = result.as_ref().unwrap().allocation.unwrap();
            assert!(report.actual_bytes > 0);
            assert!(
                report.cleanup_bytes > 0,
                "outer recording must stay tagged: {report:?}"
            );
            assert_eq!(report.excess_bytes, 0);
        }
        assert_eq!(run.join("A/written.txt").exists(), written, "{mode}");
        assert!(!run.join("B/denied.txt").exists());
        assert!(!run.join("after.txt").exists());
        drop(tools);
        drop(writer);
        let reader = SessionReader::open(&path).unwrap();
        let limit = smith::config::Config::default_valid().frame_bytes_max;
        reader.validate_work(limit).unwrap();
        if written && mode != "recording-fail-catch" {
            let mut records = Vec::new();
            reader
                .scan(limit, |frame| {
                    if let Frame::Known { entry } | Frame::NestedCommit { entry } = frame
                        && let EntryContent::Meta { kind, detail } =
                            EntryContent::decode(&entry.content)?
                        && matches!(
                            kind.as_str(),
                            smith_core::metadata::NESTED_INTENT_KIND
                                | smith_core::metadata::NESTED_COMPLETION_KIND
                        )
                    {
                        records.push(serde_json::to_value(detail).unwrap());
                    }
                    Ok(())
                })
                .unwrap();
            assert!(records.len() >= 4, "{mode}: {records:?}");
            assert_eq!(
                records[1]["causal"]["parent"],
                records[0]["causal"]["effect_id"]
            );
            assert_eq!(records[2]["causal"], records[1]["causal"]);
            assert_eq!(records[0]["component"]["source_id"], "fixture:target");
            assert_eq!(records[1]["component"]["source_id"], "fixture:target");
        }
    }
    std::fs::remove_dir_all(dir).unwrap();
}

#[test]
fn composition_siblings_charge_freed_persistence_allocations_once_to_root() {
    use std::sync::Mutex;
    const PERSISTENCE_BYTES: usize = 4 * 1024 * 1024;
    struct Dispatch {
        registry: PluginRegistry,
        grants: CompositionGrants,
    }
    impl ToolDispatcher for Dispatch {
        fn invoke(
            &mut self,
            _: &str,
            _: &ToolArguments,
            effects: &mut ToolEffectContext<'_>,
        ) -> Result<String> {
            self.registry.invoke_composed(
                "fixture:a/fixture",
                r#"{"mode":"siblings"}"#,
                effects,
                &self.grants,
                &ToolPolicy::default(),
            )
        }
    }
    let dir = std::env::temp_dir().join(format!(
        "smith-sibling-allocation-{}",
        smith::id::SessionId::new()
    ));
    std::fs::create_dir(&dir).unwrap();
    let artifact =
        fixture_component_source(&dir, include_str!("../../tests/fixtures/composition.rs"));
    let engine = PluginEngine::new().unwrap();
    let mut first_completion_bytes = 0;
    for limited in [false, true] {
        let run = dir.join(if limited { "limited" } else { "control" });
        std::fs::create_dir_all(run.join("A")).unwrap();
        let mut registry = PluginRegistry::new();
        registry.publish_all(["a", "target"].map(|id| {
            let manifest = PluginManifest::parse(&format!("id \"fixture:{id}\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"guest.wasm\"\ncapability \"workspace\"\n")).unwrap();
            PluginGeneration::load(&engine, manifest, &artifact, &[Capability::Workspace]).unwrap()
        }).into()).unwrap();
        let mut grants =
            CompositionGrants::from_specs(&["fixture:a/fixture=fixture:target/fixture#A".into()])
                .unwrap();
        if limited {
            grants = grants
                .with_host_allocation_limit(first_completion_bytes + PERSISTENCE_BYTES as u64 / 2)
                .unwrap();
        }
        let path = run.join("session.smh");
        let (tools, writer) = admitted_tools(&path, Box::new(Dispatch { registry, grants }), true);
        let mut submission = None;
        SessionReader::open(&path)
            .unwrap()
            .scan(smith::Config::default_valid().frame_bytes_max, |frame| {
                if let Frame::Claim { submission_id } = frame {
                    submission = Some(submission_id);
                }
                Ok(())
            })
            .unwrap();
        let observations = Arc::new(Mutex::new(Vec::with_capacity(2)));
        let observed = observations.clone();
        let allocation = Arc::new(Mutex::new(None));
        let captured = allocation.clone();
        let committed = writer.clone();
        let mut tools = tools.with_completion_boundary(submission.unwrap(), move |frame| {
            let child_completion = if let Frame::NestedCommit { entry } = &frame {
                matches!(EntryContent::decode(&entry.content).unwrap(), EntryContent::Meta { kind, detail }
                    if kind == smith_core::metadata::NESTED_COMPLETION_KIND
                    && serde_json::to_value(&detail).unwrap()["output"] == "child complete")
            } else { false };
            if child_completion {
                let group = smith_alloc::AllocationGroup::current().expect("persistence must retain the root allocation group");
                let before = group.allocated().unwrap();
                let bytes = vec![0x5a_u8; PERSISTENCE_BYTES];
                std::hint::black_box(&bytes);
                drop(bytes);
                let after = group.allocated().unwrap();
                assert_eq!(after.bytes - before.bytes, PERSISTENCE_BYTES as u64, "freed persistence allocation must count exactly once");
                assert_eq!(after.allocations - before.allocations, 1);
                observed.lock().unwrap().push(after.bytes);
                *captured.lock().unwrap() = Some(group.clone());
                let before_commit = group.allocated().unwrap();
                let local = smith_alloc::checkpoint().unwrap();
                committed.commit_completion(frame)?;
                let local = smith_alloc::allocated_since(&local).unwrap();
                let after_commit = group.allocated().unwrap();
                assert!(after_commit.bytes - before_commit.bytes > local.bytes,
                    "real storage worker allocations must join the same root group: {before_commit:?} {after_commit:?} {local:?}");
                return Ok(());
            }
            committed.commit_completion(frame)
        });
        let result = tools.invoke("fixture", &ToolArguments::empty_object());
        let measured = observations.lock().unwrap();
        assert_eq!(
            measured.len(),
            2,
            "both actual sibling completions must reach persistence"
        );
        first_completion_bytes = measured[0];
        assert!(measured[1] >= measured[0] + PERSISTENCE_BYTES as u64);
        let actual = allocation
            .lock()
            .unwrap()
            .as_ref()
            .unwrap()
            .allocated()
            .unwrap();
        assert!(actual.bytes >= measured[1]);
        if limited {
            let error = result.unwrap_err().to_string();
            assert!(
                error.contains("terminal host invocation stop: HostAllocation"),
                "{error}"
            );
        } else {
            let result = result.unwrap();
            assert!(result.ok);
            let report = result.allocation.unwrap();
            assert_eq!(
                report.actual_bytes, actual.bytes,
                "reported total must equal the actual shared observer including cleanup"
            );
            assert!(report.cleanup_bytes > 0);
            assert_eq!(report.excess_bytes, 0);
        }
        assert_eq!(run.join("after.txt").exists(), !limited);
        assert!(run.join("A/written.txt").exists());
        drop(measured);
        drop(tools);
        drop(writer);
        SessionReader::open(&path)
            .unwrap()
            .validate_work(smith::Config::default_valid().frame_bytes_max)
            .unwrap();
    }
    std::fs::remove_dir_all(dir).unwrap();
}

#[cfg(unix)]
#[test]
fn agent_nested_intent_ack_pins_child_generation_until_next_provider_round() {
    use futures::StreamExt;
    use smith::stream::{StopReason, StreamEvent};
    use std::sync::atomic::{AtomicUsize, Ordering};
    let dir = std::env::temp_dir().join(format!(
        "smith-nested-reload-{}",
        smith::id::SessionId::new()
    ));
    std::fs::create_dir_all(dir.join("A")).unwrap();
    let source = include_str!("../../tests/fixtures/composition.rs");
    let old_build = dir.join("old");
    let new_build = dir.join("new");
    std::fs::create_dir(&old_build).unwrap();
    std::fs::create_dir(&new_build).unwrap();
    let old_artifact = fixture_component_source(
        &old_build,
        &source.replace("child committed", "old artifact"),
    );
    let new_artifact = fixture_component_source(
        &new_build,
        &source.replace("child committed", "new artifact"),
    );
    let packages = PackageRegistry::at(dir.join("registry"));
    let engine = PluginEngine::new().unwrap();
    for id in ["a", "target"] {
        let package = dir.join(id);
        std::fs::create_dir(&package).unwrap();
        std::fs::copy(&old_artifact, package.join("guest.wasm")).unwrap();
        std::fs::write(package.join("plugin.kdl"), format!("id \"fixture:{id}\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"guest.wasm\"\ncapability \"workspace\"\n")).unwrap();
        packages
            .link(&engine, &package, &[Capability::Workspace])
            .unwrap();
    }
    let (control, request_reload) = ReloadControl::test_notifications();
    let dispatcher =
        runtime_tools_dispatcher(&dir, AuthStore::at(dir.join("auth")), false, Some(control))
            .with_package_registry(packages)
            .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 = dir.join("session.smh");
    let (tools, writer) = admitted_tools(&path, Box::new(dispatcher), true);
    let mut submission = None;
    SessionReader::open(&path)
        .unwrap()
        .scan(smith::Config::default_valid().frame_bytes_max, |frame| {
            if let Frame::Claim { submission_id } = frame {
                submission = Some(submission_id);
            }
            Ok(())
        })
        .unwrap();
    let changed = Arc::new(AtomicUsize::new(0));
    let changed_at_ack = changed.clone();
    let target_artifact = dir.join("target/guest.wasm");
    let committed = writer.clone();
    let tools = tools.with_completion_boundary(submission.unwrap(), move |frame| {
        if let Frame::Known { entry } = &frame
            && let EntryContent::Meta { kind, detail } =
                EntryContent::decode(&entry.content).unwrap()
            && kind == smith_core::metadata::NESTED_INTENT_KIND
            && serde_json::to_value(&detail).unwrap()["operation"] == "fixture:target/fixture"
            && changed_at_ack.fetch_add(1, Ordering::SeqCst) == 0
        {
            // Exact host persistence ACK barrier: child is selected, but cannot run yet.
            std::fs::copy(&new_artifact, &target_artifact).unwrap();
            request_reload();
        }
        committed.commit_completion(frame)
    });
    let rounds = Arc::new(AtomicUsize::new(0));
    let requested = rounds.clone();
    let workspace = dir.clone();
    let stream: smith::StreamFn = Arc::new(move |_, _| {
        let round = requested.fetch_add(1, Ordering::SeqCst);
        if round > 0 {
            assert_eq!(
                std::fs::read_to_string(workspace.join("A/written.txt")).unwrap(),
                if round == 1 {
                    "old artifact"
                } else {
                    "new artifact"
                }
            );
        }
        futures::stream::iter(if round < 2 {
            vec![
                Ok(StreamEvent::tool_use(
                    smith::id::MessageId::new(),
                    "fixture:a/fixture",
                    ToolArguments::from_json(r#"{"mode":"siblings"}"#).unwrap(),
                    smith::id::ToolCallId::new(),
                )),
                Ok(StreamEvent::stop(StopReason::ToolUse, None)),
            ]
        } else {
            vec![Ok(StreamEvent::stop(StopReason::EndTurn, None))]
        })
        .boxed()
    });
    let mut agent = smith_core::agent::Agent::new(stream, tools, "mock");
    let settled = writer.clone();
    agent
        .run_admitted(submission.unwrap(), move |frame| settled.settle(frame))
        .unwrap();
    assert_eq!(rounds.load(Ordering::SeqCst), 3);
    assert_eq!(changed.load(Ordering::SeqCst), 4);
    drop(agent);
    drop(writer);
    let reader = SessionReader::open(&path).unwrap();
    let limit = smith::Config::default_valid().frame_bytes_max;
    reader.validate_work(limit).unwrap();
    let mut roots = Vec::new();
    let mut writes = Vec::new();
    let mut children = Vec::new();
    let mut completions = Vec::new();
    reader
        .scan(limit, |frame| {
            if let Frame::Known { entry } = &frame
                && let smith_core::session::EntryKind::ToolCall { call_id } = entry.kind
            {
                roots.push((entry.id, serde_json::to_value(call_id).unwrap()));
            }
            if let Frame::Known { entry } | Frame::NestedCommit { entry } = frame
                && let EntryContent::Meta { kind, detail } = EntryContent::decode(&entry.content)?
            {
                let detail = serde_json::to_value(detail).unwrap();
                if kind == smith_core::metadata::NESTED_INTENT_KIND
                    && detail["operation"] == "fixture:target/fixture"
                {
                    children.push((entry.id, detail));
                } else if kind == smith_core::metadata::NESTED_INTENT_KIND
                    && detail["operation"] == "write"
                {
                    writes.push(detail);
                } else if kind == smith_core::metadata::NESTED_COMPLETION_KIND {
                    completions.push(detail);
                }
            }
            Ok(())
        })
        .unwrap();
    assert_eq!(children.len(), 4);
    assert_eq!(roots.len(), 2);
    assert_ne!(roots[0].0, roots[1].0);
    let old_hash = &children[0].1["component"]["component_sha256"];
    let new_hash = &children[2].1["component"]["component_sha256"];
    assert_ne!(old_hash, new_hash);
    for (index, (id, detail)) in children.iter().enumerate() {
        assert_eq!(
            &detail["component"]["component_sha256"],
            if index < 2 { old_hash } else { new_hash }
        );
        assert_eq!(
            detail["causal"]["effect_id"],
            serde_json::to_value(id).unwrap()
        );
        assert_eq!(
            detail["submission_id"],
            serde_json::to_value(submission.unwrap()).unwrap()
        );
        assert_eq!(detail["call_id"], roots[index / 2].1);
        assert_ne!(*id, roots[index / 2].0);
        assert!(writes.iter().any(|write| write["causal"]["parent"]
            == serde_json::to_value(id).unwrap()
            && write["call_id"] == detail["call_id"]
            && write["component"] == detail["component"]));
        assert!(
            completions
                .iter()
                .any(|completion| completion["causal"] == detail["causal"]
                    && completion["call_id"] == detail["call_id"])
        );
    }
    assert_eq!(children[0].1["call_id"], children[1].1["call_id"]);
    assert_eq!(children[2].1["call_id"], children[3].1["call_id"]);
    assert_ne!(children[0].1["call_id"], children[2].1["call_id"]);
    assert_eq!(
        children[0].1["causal"]["parent"],
        children[1].1["causal"]["parent"]
    );
    std::fs::remove_dir_all(dir).unwrap();
}

#[test]
fn composition_wasi_handles_share_root_ceiling_across_suspended_parent_and_child() {
    struct Dispatch {
        registry: PluginRegistry,
        grants: CompositionGrants,
        input: &'static str,
        retained: bool,
    }
    impl ToolDispatcher for Dispatch {
        fn invoke(
            &mut self,
            _: &str,
            _: &ToolArguments,
            effects: &mut ToolEffectContext<'_>,
        ) -> Result<String> {
            let result = self.registry.invoke_composed(
                "fixture:a/fixture",
                self.input,
                effects,
                &self.grants,
                &ToolPolicy::default(),
            );
            if result.is_ok() {
                assert!(
                    self.registry.generations.iter().all(|generation| generation
                        .instance
                        .store
                        .data()
                        .sealed_handles
                        == u32::from(self.retained)),
                    "actual guest resource destructors must return live-handle budget"
                );
            } else {
                assert_eq!(
                    effects.stop_reason(),
                    Some(smith_core::tools::InvocationStop::Sandbox)
                );
            }
            result
        }
    }
    let dir = std::env::temp_dir().join(format!(
        "smith-wasi-handles-{}",
        smith::id::SessionId::new()
    ));
    std::fs::create_dir(&dir).unwrap();
    let world = r#"inline: "package fixture:handle-test; world handle-world {
        include smith:plugin/plugin-world@0.1.0;
        import wasi:cli/stdout@0.2.12;
        import wasi:cli/stdin@0.2.12;
    }", world: "handle-world", generate_all,"#;
    let mode = r##"
        if input.contains("handle-retain") {
            core::mem::forget(wasi::cli::stdout::get_stdout());
            return Ok("retained real resource".into());
        }
        if input.contains("handle-parent") || input.contains("handle-reuse-parent") {
            let held = core::array::from_fn::<_, 4, _>(|_| wasi::cli::stdout::get_stdout());
            let result = smith::plugin::composition::invoke("fixture:target/fixture", if input.contains("reuse") { r#"{"mode":"handle-reuse-child"}"# } else { r#"{"mode":"handle-child"}"# });
            std::hint::black_box(&held);
            return result;
        }
        if input.contains("handle-reuse-child") {
            for _ in 0..4 {
                let held = core::array::from_fn::<_, 2, _>(|_| wasi::cli::stdout::get_stdout());
                std::hint::black_box(&held);
            }
            smith::plugin::tooling::write(&smith::plugin::tooling::FileWrite { path: "after.txt".into(), content: "reused live WASI handles".into() })?;
            return Ok("reused live WASI handles".into());
        }
        if input.contains("handle-child") {
            let held = core::array::from_fn::<_, 4, _>(|_| wasi::cli::stdin::get_stdin());
            smith::plugin::tooling::write(&smith::plugin::tooling::FileWrite { path: "after.txt".into(), content: "eight live WASI handles".into() })?;
            std::hint::black_box(&held);
            return Ok("eight live WASI handles".into());
        }
"##;
    let source = include_str!("../../tests/fixtures/composition.rs")
        .replace("world: \"plugin-world\",", world)
        .replace(
            "        if input.contains(\"call-count\") {",
            &[mode, "        if input.contains(\"call-count\") {"].concat(),
        )
        .replace(
            "WIT_PATH",
            Path::new(env!("CARGO_MANIFEST_DIR"))
                .parent()
                .unwrap()
                .join("wit")
                .to_str()
                .unwrap(),
        );
    let artifact = fixture_component_source(&dir, &source);
    let engine = PluginEngine::new().unwrap();
    for (handles, reuse, retained) in [
        (12, false, false),
        (6, true, false),
        (8, true, true),
        (8, false, true),
        (6, false, false),
    ] {
        let run = dir.join(format!("{handles}-{reuse}-{retained}"));
        std::fs::create_dir_all(run.join("A")).unwrap();
        let mut registry = PluginRegistry::new();
        registry.publish_all(["a", "target"].map(|id| {
            let manifest = PluginManifest::parse(&format!("id \"fixture:{id}\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"guest.wasm\"\ncapability \"workspace\"\n")).unwrap();
            let mut generation = PluginGeneration::load(&engine, manifest, &artifact, &[Capability::Workspace]).unwrap();
            if retained {
                let mut session = ToolSession::new(&run);
                let mut effects = ToolEffectContext::for_operation(&mut session, "retain");
                generation.invoke_with_effects("fixture", r#"{"mode":"handle-retain"}"#, &mut effects).unwrap();
                assert_eq!(generation.instance.store.data().sealed_handles, 1);
            }
            generation
        }).into()).unwrap();
        let grants =
            CompositionGrants::from_specs(&["fixture:a/fixture=fixture:target/fixture#A".into()])
                .unwrap()
                .with_resource_limits(CompositionResourceLimits {
                    memory_bytes: 64 * 1024 * 1024,
                    table_elements: 10_000,
                    conversion_bytes: 8 * 1024 * 1024,
                    output_bytes: 1024 * 1024,
                    component_handles: handles,
                    calls: 32,
                    depth: 8,
                })
                .unwrap();
        let (mut tools, writer) = admitted_tools(
            &run.join("session.smh"),
            Box::new(Dispatch {
                registry,
                grants,
                retained,
                input: if reuse {
                    r#"{"mode":"handle-reuse-parent"}"#
                } else {
                    r#"{"mode":"handle-parent"}"#
                },
            }),
            true,
        );
        let result = tools.invoke("fixture", &ToolArguments::empty_object());
        if handles == 12 || reuse {
            assert!(
                result.as_ref().is_ok_and(|result| result.ok),
                "generous control must reach real child: {result:?}"
            );
            assert_eq!(
                std::fs::read_to_string(run.join("A/after.txt")).unwrap(),
                if reuse {
                    "reused live WASI handles"
                } else {
                    "eight live WASI handles"
                }
            );
        } else {
            assert!(
                !run.join("A/after.txt").exists(),
                "shared handle ceiling must include retained resources + four parent + four child handles before write; {result:?}"
            );
            assert!(!result.as_ref().is_ok_and(|result| result.ok));
        }
        drop(tools);
        drop(writer);
    }
    std::fs::remove_dir_all(dir).unwrap();
}

#[test]
fn composition_rejects_component_private_resource_new_before_guest_effects() {
    let dir = std::env::temp_dir().join(format!(
        "smith-private-resource-{}",
        smith::id::SessionId::new()
    ));
    std::fs::create_dir(&dir).unwrap();
    let world = r#"inline: "package fixture:private-resource; interface private-resource { resource token { constructor(); } } world private-world { include smith:plugin/plugin-world@0.1.0; export private-resource; }", world: "private-world", generate_all,"#;
    let source = include_str!("../../tests/fixtures/composition.rs").replace("world: \"plugin-world\",", world).replace("struct EchoPlugin;", "struct EchoPlugin;\n\nuse exports::fixture::private_resource::private_resource::{Guest as PrivateResourceGuest, GuestToken};\n\nstruct PrivateToken;\n\nimpl GuestToken for PrivateToken {\n    fn new() -> Self { Self }\n}\n\nimpl PrivateResourceGuest for EchoPlugin {\n    type Token = PrivateToken;\n}");
    let artifact = fixture_component_source(&dir, &source);
    let engine = PluginEngine::new().unwrap();
    let generation = |id: &str| {
        let manifest = PluginManifest::parse(&format!("id \"fixture:{id}\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"guest.wasm\"\ncapability \"workspace\"\n")).unwrap();
        PluginGeneration::load(&engine, manifest, &artifact, &[Capability::Workspace]).unwrap()
    };
    assert!(generation("fact").component_private_resources);
    let mut registry = PluginRegistry::new();
    registry
        .publish_all(vec![generation("a"), generation("target")])
        .unwrap();
    let grants =
        CompositionGrants::from_specs(&["fixture:a/fixture=fixture:target/fixture#.".into()])
            .unwrap();
    let mut session = ToolSession::new(&dir);
    let mut effects = ToolEffectContext::for_operation(&mut session, "private-resource");
    let result = registry.invoke_composed(
        "fixture:a/fixture",
        r#"{"mode":"caller"}"#,
        &mut effects,
        &grants,
        &ToolPolicy::default(),
    );
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("canonical component-private resource.new")
    );
    assert!(!dir.join("written.txt").exists());
    drop(effects);
    let mut ordinary = generation("ordinary");
    let mut ordinary_session = ToolSession::new(&dir);
    let mut ordinary_effects =
        ToolEffectContext::for_operation(&mut ordinary_session, "ordinary-resource");
    assert_eq!(
        ordinary
            .invoke_with_effects(
                "fixture",
                r#"{"mode":"output-budget"}"#,
                &mut ordinary_effects
            )
            .unwrap(),
        "xx"
    );
    std::fs::remove_dir_all(dir).unwrap();
}