//! SMH-SPEC-PLUG0001: real lifecycle components alter next wire request in eval and RPC. //! SMH-SPEC-SPEC0001: durable request context and paired tool results. use assert_cmd::Command; use std::io::{BufRead, BufReader, Read, Write}; use std::net::{TcpListener, TcpStream}; use std::path::Path; use std::time::Duration; #[path = "../../smith-harness/tests/fixtures/lifecycle_support.rs"] mod support; fn smith(home: &Path) -> std::process::Command { let mut command = std::process::Command::new(assert_cmd::cargo::cargo_bin("smith")); command .current_dir(home) .env("HOME", home) .env("XDG_CONFIG_HOME", home.join("config")) .env("XDG_DATA_HOME", home.join("data")) .env("XDG_STATE_HOME", home.join("state")) .env("XDG_CACHE_HOME", home.join("cache")) .env("XDG_RUNTIME_DIR", home.join("runtime")) .env_remove("SMITH_PROVIDER") .env_remove("SMITH_MODEL") .env_remove("SMITH_BASE_URL") .env_remove("ANTHROPIC_API_KEY") .env_remove("OPENAI_API_KEY") .env_remove("GEMINI_API_KEY") .env_remove("GOOGLE_API_KEY"); command } fn body(stream: &mut TcpStream) -> serde_json::Value { stream .set_read_timeout(Some(Duration::from_secs(10))) .unwrap(); stream .set_write_timeout(Some(Duration::from_secs(10))) .unwrap(); let mut request = Vec::new(); let mut buffer = [0; 4096]; let header_end = loop { let count = stream.read(&mut buffer).unwrap(); assert!(count > 0 && request.len() + count <= 1 << 20); request.extend_from_slice(&buffer[..count]); if let Some(index) = request.windows(4).position(|bytes| bytes == b"\r\n\r\n") { break index + 4; } }; let headers = std::str::from_utf8(&request[..header_end]).unwrap(); let length: usize = headers .lines() .find_map(|line| { line.to_ascii_lowercase() .strip_prefix("content-length: ") .and_then(|value| value.trim().parse().ok()) }) .unwrap(); assert!(header_end + length <= 1 << 20); while request.len() < header_end + length { let count = stream.read(&mut buffer).unwrap(); assert!(count > 0); request.extend_from_slice(&buffer[..count]); } let body: serde_json::Value = serde_json::from_slice(&request[header_end..header_end + length]).unwrap(); body } fn provider(rounds: usize, tool_followup: bool) -> (std::net::SocketAddr, Server) { provider_with_tool( rounds, tool_followup.then_some(("write", r#"{"path":"proof.txt","content":"evidence"}"#)), ) } fn provider_with_tool(rounds: usize, tool: Option<(&str, &str)>) -> (std::net::SocketAddr, Server) { provider_tool_script(rounds, tool, false) } fn provider_tool_script( rounds: usize, tool: Option<(&str, &str)>, fail_followup: bool, ) -> (std::net::SocketAddr, Server) { let tool = tool.map(|(name, arguments)| (name.to_string(), arguments.to_string())); let listener = TcpListener::bind("127.0.0.1:0").unwrap(); let address = listener.local_addr().unwrap(); let (sender, receiver) = std::sync::mpsc::sync_channel(1); let (first_sender, first_request) = std::sync::mpsc::sync_channel(1); let worker = std::thread::spawn(move || { let mut requests = Vec::new(); for text in ["first", "continued", "last", "last"] .into_iter() .take(rounds) { let (mut stream, _) = listener.accept().unwrap(); requests.push(body(&mut stream)); if text == "first" { first_sender.send(()).unwrap(); } if fail_followup && text == "continued" { stream.write_all(b"HTTP/1.1 503 Unavailable\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}").unwrap(); continue; } let response = if let Some((name, arguments)) = &tool && text == "first" { format!( "data: {}\n\ndata: {}\n\ndata: [DONE]\n\n", serde_json::json!({"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_lifecycle","type":"function","function":{"name":name,"arguments":arguments}}]},"finish_reason":null}]}), serde_json::json!({"choices":[{"delta":{},"finish_reason":"tool_calls"}]}) ) } else { format!( "data: {{\"choices\":[{{\"delta\":{{\"content\":\"{text}\"}}}}]}}\n\ndata: {{\"choices\":[{{\"delta\":{{}},\"finish_reason\":\"stop\"}}]}}\n\ndata: [DONE]\n\n" ) }; write!(stream, "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\nContent-Length: {}\r\n\r\n{response}", response.len()).unwrap(); } sender.send(requests).unwrap(); }); ( address, Server { address, receiver, first_request, worker: Some(worker), }, ) } struct Server { address: std::net::SocketAddr, receiver: std::sync::mpsc::Receiver>, first_request: std::sync::mpsc::Receiver<()>, worker: Option>, } impl Server { fn finish(mut self) -> Vec { let requests = self .receiver .recv_timeout(Duration::from_secs(15)) .expect("provider fixture must receive all expected requests before deadline"); self.worker.take().unwrap().join().unwrap(); requests } } impl Drop for Server { fn drop(&mut self) { if let Some(worker) = self.worker.take() { // Explicit shutdown signals unblock accept/read even when startup failed. for _ in 0..2 { let _wake = TcpStream::connect_timeout(&self.address, Duration::from_secs(1)); } let _joined = worker.join(); } } } fn install_decision(home: &Path, id: &str, actions: &str, required: bool) { install_boundary_decision(home, id, actions, required, "TurnComplete"); } fn install_boundary_decision(home: &Path, id: &str, actions: &str, required: bool, boundary: &str) { let source = include_str!("../../smith-harness/tests/fixtures/lifecycle.rs") .replace("Boundary::TurnComplete", &format!("Boundary::{boundary}")) .replace("vec![Action::EnsureContinuation]", actions) .replace( "required_decision: false", &format!("required_decision: {required}"), ); let source = if actions.starts_with("return Err") { source.replace( " use exports::smith::plugin::lifecycle::*;\n Ok(if", " Ok(if", ) } else { source }; let source = if actions.contains("SetToolPolicy") { source.replace("fn declare_tools() -> Vec {\n vec![]", "fn declare_tools() -> Vec {\n vec![plugin::ToolDeclaration { name: \"controlled\".into(), description: \"controlled fixture\".into(), input_schema_json: \"{}\".into() }]") } else { source }; let name = id.split_once(':').unwrap().1; let artifact = support::compile_fixture(&home.join(name), &source, &format!("lifecycle-{name}")); let registry = smith_harness::plugins::registry::PackageRegistry::at(home.join("config/smith/plugins")); support::install( ®istry, &smith_harness::wasm::PluginEngine::new().unwrap(), &home.join(format!("package-{name}")), id, &artifact, ); } fn assert_next_request(requests: &[serde_json::Value]) { assert_eq!(requests.len(), 2); assert!( requests[0]["messages"] .as_array() .unwrap() .iter() .any(|message| message["content"] == "raw-root") ); assert!(!requests[0].to_string().contains("next-request-prompt")); let next = requests[1].to_string(); assert!(next.contains("effective-root")); assert!(next.contains("next-request-prompt")); assert!(!next.contains("raw-root")); } #[test] fn eval_two_sandbox_decisions_change_only_the_single_next_wire_request() { eval_continuation(false); } #[test] fn eval_eligible_tool_followup_satisfies_both_continuations_with_paired_wire_results() { eval_continuation(true); } fn eval_continuation(tool_followup: bool) { let home = std::env::temp_dir().join(format!("smith-lifecycle-eval-{}", std::process::id())); std::fs::create_dir_all(&home).unwrap(); support::install_consumers(&home, &home.join("config/smith/plugins")); install_decision( &home, "example:fault", "return Err(\"private-error\".into())", false, ); install_decision( &home, "example:settings", "vec![Action::IncludeUsage(false), Action::SetModel(\"test-model\".into()), Action::SetProvider(\"openai-compatible-api\".into()), Action::SetToolPolicy(vec![ToolPolicyChange { name: \"example:settings/controlled\".into(), availability: Availability::Hidden }])]", false, ); let (address, worker) = provider(2, tool_followup); let output = Command::from_std(smith(&home)) .timeout(Duration::from_secs(30)) .env("OPENAI_API_KEY", "lifecycle-fixture-secret") .args([ "eval", "--provider", "openai", "--model", "test-model", "--base-url", &format!("http://{address}/v1"), "raw-root", ]) .output() .unwrap(); if !output.status.success() { for _ in 0..2 { let _wake = TcpStream::connect_timeout(&address, Duration::from_secs(1)); } } let requests = worker.finish(); assert!( output.status.success(), "{}", String::from_utf8_lossy(&output.stderr) ); assert_eq!(output.stdout, b"continued\n"); assert_next_request(&requests); if tool_followup { assert_eq!( std::fs::read_to_string(home.join("proof.txt")).unwrap(), "evidence" ); let messages = requests[1]["messages"].as_array().unwrap(); let call = messages .iter() .find(|message| message["role"] == "assistant" && message["tool_calls"].is_array()) .unwrap(); let result = messages .iter() .find(|message| message["role"] == "tool") .unwrap(); assert_eq!(call["tool_calls"][0]["id"], result["tool_call_id"]); assert_eq!( messages .iter() .filter(|message| message["role"] == "tool") .count(), 1 ); } assert!( requests[0]["tools"] .as_array() .unwrap() .iter() .any(|tool| tool["function"]["name"] == "example:settings/controlled") ); assert!( !requests[1]["tools"] .as_array() .unwrap() .iter() .any(|tool| tool["function"]["name"] == "example:settings/controlled") ); assert_eq!(requests[1]["model"], "test-model"); assert_eq!(requests[0]["stream_options"]["include_usage"], true); assert_eq!(requests[1]["stream_options"]["include_usage"], false); assert!(!String::from_utf8_lossy(&output.stdout).contains("lifecycle-fixture-secret")); let sessions = std::fs::read_dir(home.join("data/smith/sessions/run")).unwrap(); let path = sessions .filter_map(Result::ok) .map(|entry| entry.path()) .find(|path| path.extension().is_some_and(|extension| extension == "smh")) .unwrap(); let session = smith_core::store::SessionReader::open(&path) .unwrap() .selected_session(smith::Config::default_valid().frame_bytes_max) .unwrap(); let raw: Vec<_> = session .active_branch() .entries() .iter() .map(|entry| session.content(entry).unwrap()) .collect(); assert!(format!("{raw:?}").contains("raw-root")); assert!(format!("{raw:?}").contains("lifecycle_decision")); let root_id = session.active_branch().entries().iter().find(|entry| matches!(session.content(entry).unwrap(), smith_core::session::EntryContent::Message(message) if message.role == smith::Role::User)).unwrap().id; let records: Vec<_> = raw .iter() .filter_map(|entry| match entry { smith_core::session::EntryContent::Meta { kind, detail } if kind == "lifecycle_decision" => { Some( detail .deserialize_as::<( smith::lifecycle::LifecycleEvent, smith::lifecycle::LifecycleBatch, )>() .unwrap(), ) } _ => None, }) .collect(); let trace = records .iter() .map(|(event, batch)| { assert_eq!(event.root_entry_id, root_id); let names = batch .actions .iter() .map(|action| match serde_json::to_value(action).unwrap() { serde_json::Value::String(name) => name, serde_json::Value::Object(object) => object.into_iter().next().unwrap().0, _ => unreachable!(), }) .collect::>() .join("|"); let diagnostics = batch .diagnostics .iter() .map(|diagnostic| format!("{:?}", diagnostic.code)) .collect::>() .join("|"); format!( "{:?}[{}] {:?} root=$root eligible={} actions={names} diagnostics={diagnostics}\n", event.boundary, event.round_index, event.last_stop, event.continuation_eligible ) }) .collect::(); if tool_followup { insta::assert_snapshot!(trace, @r#" TurnComplete[0] ToolUse root=$root eligible=true actions=EnsureContinuation|EditContext|EnsureContinuation|SetPrompt|IncludeUsage|SetModel|SetProvider|SetToolPolicy diagnostics=HandlerFailed TurnComplete[1] EndTurn root=$root eligible=true actions= diagnostics= "#); } else { insta::assert_snapshot!(trace, @r#" TurnComplete[0] EndTurn root=$root eligible=true actions=EnsureContinuation|EditContext|EnsureContinuation|SetPrompt|IncludeUsage|SetModel|SetProvider|SetToolPolicy diagnostics=HandlerFailed TurnComplete[1] EndTurn root=$root eligible=true actions= diagnostics= "#); } let diagnostics: Vec<_> = raw .iter() .filter_map(|entry| match entry { smith_core::session::EntryContent::Meta { kind, detail } if kind == "lifecycle_decision" => { Some( detail .deserialize_as::<( smith::lifecycle::LifecycleEvent, smith::lifecycle::LifecycleBatch, )>() .unwrap() .1 .diagnostics, ) } _ => None, }) .flatten() .collect(); assert_eq!(diagnostics.len(), 1); insta::allow_duplicates! { insta::assert_snapshot!(serde_json::to_string_pretty(&diagnostics).unwrap(), @r#" [ { "component": "example:fault", "handler": "continue", "code": "HandlerFailed" } ] "#); } std::fs::remove_dir_all(home).unwrap(); } struct Child(std::process::Child); impl Drop for Child { fn drop(&mut self) { let _ = self.0.kill(); let _ = self.0.wait(); } } #[test] fn rpc_two_sandbox_decisions_settle_one_root_after_one_continuation() { rpc_scenario(None, false); } #[test] fn rpc_required_decision_failure_discards_context_and_continuation_with_one_terminal_error() { rpc_scenario(Some("vec![Action::SetModel(\"unavailable\".into())]"), true); } #[test] fn rpc_conflicting_context_writes_reject_the_entire_non_cancel_batch() { rpc_scenario( Some( "vec![Action::EditContext(vec![ContextEdit { target: event.root_entry_id, replacement: Some(Replacement::Text(\"conflicting-root\".into())) }])]", ), false, ); } #[test] fn rpc_conflicting_tool_availability_rejects_the_entire_non_cancel_batch() { rpc_scenario( Some( "vec![Action::SetToolPolicy(vec![ToolPolicyChange { name: \"example:conflict/controlled\".into(), availability: Availability::Hidden }, ToolPolicyChange { name: \"example:conflict/controlled\".into(), availability: Availability::Disabled }])]", ), false, ); } fn rpc_scenario(actions: Option<&str>, required: bool) { static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); use std::process::Stdio; let home = std::env::temp_dir().join(format!( "slr-{}-{}", std::process::id(), NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed) )); std::fs::create_dir_all(&home).unwrap(); support::install_consumers(&home, &home.join("config/smith/plugins")); if let Some(actions) = actions { install_decision(&home, "example:conflict", actions, required); } let (address, worker) = provider(if actions.is_some() { 1 } else { 2 }, false); let mut command = smith(&home); command .env("OPENAI_API_KEY", "lifecycle-fixture-secret") .args([ "rpc", "--provider", "openai", "--model", "test-model", "--base-url", &format!("http://{address}/v1"), ]); let mut child = Child( command .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn() .unwrap(), ); let mut input = child.0.stdin.take().unwrap(); writeln!(input, "{{\"id\":1,\"method\":\"prompt\",\"params\":{{\"input\":\"raw-root\"}}}}\n{{\"id\":2,\"method\":\"wait\"}}").unwrap(); input.flush().unwrap(); let stdout = child.0.stdout.take().unwrap(); let (sender, receiver) = std::sync::mpsc::sync_channel(16); let reader = std::thread::spawn(move || { let mut reader = BufReader::new(stdout); loop { let mut line = Vec::new(); if reader.read_until(b'\n', &mut line).unwrap() == 0 { break; } assert!( line.len() <= 65536 && line.ends_with(b"\n") && !line.contains(&b'\r'), "RPC wire must be bounded LF-only JSON lines" ); if sender.send(String::from_utf8(line).unwrap()).is_err() { break; } } }); let mut lines = Vec::new(); for _ in 0..8 { let value: serde_json::Value = serde_json::from_str(&receiver.recv_timeout(Duration::from_secs(30)).unwrap()).unwrap(); let done = value["id"] == 2; lines.push(value); if done { break; } } if required { assert_eq!( lines .iter() .filter(|line| line["error"]["code"] == "TURN_FAILED") .count(), 1 ); assert_eq!( lines.iter().filter(|line| line["event"] == "end").count(), 1 ); } else { assert_eq!( lines.iter().filter(|line| line["event"] == "end").count(), 1 ); assert!( lines.iter().any(|line| line["event"] == "end" && line["text"] == if actions.is_some() { "first" } else { "continued" }), "{lines:?}" ); } assert_eq!( lines.last().unwrap(), &serde_json::json!({"id":2,"settled":true}) ); if actions.is_none() { let mut normalized = lines.clone(); normalized[0]["receipt"]["session_id"] = "$session".into(); normalized[0]["receipt"]["submission_id"] = "$root".into(); let wire = normalized .iter() .map(|line| serde_json::to_string(line).unwrap() + "\n") .collect::(); insta::assert_snapshot!(wire, @r#" {"id":1,"event":"start","receipt":{"session_id":"$session","submission_id":"$root","destination":null,"status":"Queued","cancel_requested":false}} {"id":1,"event":"delta","text":"first"} {"id":1,"event":"delta","text":"continued"} {"id":1,"event":"end","text":"continued","reason":"EndTurn","cost":null,"cost_complete":false} {"id":2,"settled":true} "#); } drop(input); assert!(matches!( receiver.recv_timeout(Duration::from_secs(10)), Err(std::sync::mpsc::RecvTimeoutError::Disconnected) )); assert!(child.0.wait().unwrap().success()); reader.join().unwrap(); let requests = worker.finish(); if actions.is_none() { assert_next_request(&requests); } else { assert_eq!(requests.len(), 1); let path = std::fs::read_dir(home.join("data/smith/sessions/run")) .unwrap() .filter_map(Result::ok) .map(|entry| entry.path()) .find(|path| path.extension().is_some_and(|extension| extension == "smh")) .unwrap(); let session = smith_core::store::SessionReader::open(&path) .unwrap() .selected_session(smith::Config::default_valid().frame_bytes_max) .unwrap(); let projected = format!("{:?}", session.projected_messages().unwrap()); assert!(projected.contains("raw-root")); assert!(!projected.contains("effective-root")); assert!(!projected.contains("next-request-prompt")); } std::fs::remove_dir_all(home).unwrap(); } #[test] fn eval_pre_settlement_handler_performs_one_durably_owned_http_effect() { eval_lifecycle_http_effect("PreSettlement"); } #[test] fn eval_settled_handler_performs_one_durably_owned_http_effect() { eval_lifecycle_http_effect("Settled"); } fn eval_lifecycle_http_effect(boundary: &str) { let home = std::env::temp_dir().join(format!("sle-{:x}", smith::SessionId::new().as_u128())); std::fs::create_dir_all(&home).unwrap(); let (address, worker) = provider(1, false); let effect_listener = TcpListener::bind("127.0.0.1:0").unwrap(); let effect_address = effect_listener.local_addr().unwrap(); let origin = format!("http://{effect_address}"); let (sent, received) = std::sync::mpsc::sync_channel(1); let server_home = home.clone(); let effect_worker = std::thread::spawn(move || { let (mut stream, _) = effect_listener.accept().unwrap(); stream .set_read_timeout(Some(Duration::from_secs(5))) .unwrap(); let mut bytes = [0u8; 4096]; let length = stream.read(&mut bytes).unwrap(); assert!(length > 0); let path = std::fs::read_dir(server_home.join("data/smith/sessions/run")) .unwrap() .filter_map(Result::ok) .map(|entry| entry.path()) .find(|path| path.extension().is_some_and(|ext| ext == "smh")) .unwrap(); let session = smith_core::store::SessionReader::open(&path) .unwrap() .selected_session(smith::Config::default_valid().frame_bytes_max) .unwrap(); assert!(session.active_branch().entries().iter().any(|entry| matches!(session.content(entry).unwrap(), smith_core::session::EntryContent::Meta { kind, .. } if kind == "lifecycle-effect-intent"))); stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 23\r\nConnection: close\r\n\r\nlifecycle-effect-secret").unwrap(); sent.send(()).unwrap(); }); let source = include_str!("../../smith-harness/tests/fixtures/lifecycle.rs") .replace("Boundary::TurnComplete", &format!("Boundary::{boundary}")) .replace("vec![Action::EnsureContinuation]", &format!(r#"{{ let mut request = smith::plugin::net_scope::HttpRequest {{ destination: "{origin}".into(), method: "POST".into(), headers: vec![], body: b"lifecycle-effect-secret".to_vec(), options: smith::plugin::net_scope::RequestOptions {{ timeout_ms: 1000, response_bytes_max: 256 }}, }}; request.options.response_bytes_max = 257; if smith::plugin::net_scope::request(&request).is_ok() {{ return Err("oversized response cap accepted".into()); }} request.options.response_bytes_max = 256; let response = smith::plugin::net_scope::request(&request)?; if response.status != 200 {{ return Err("HTTP failed".into()); }} vec![] }}"#)); let source = source.replace( " use exports::smith::plugin::lifecycle::*;\n Ok(if", " Ok(if", ); let artifact = support::compile_fixture(&home.join("guest"), &source, "lifecycle-effect"); let package = home.join("package"); std::fs::create_dir_all(&package).unwrap(); std::fs::copy(artifact, package.join("fixture.wasm")).unwrap(); std::fs::write(package.join("plugin.kdl"), "id \"example:lifecycle-http\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"fixture.wasm\"\ncapability \"net-scope\"\n").unwrap(); let install = Command::from_std(smith(&home)) .args([ "plugin", "install", package.to_str().unwrap(), "--grant", "net-scope", "--net-origin", &origin, ]) .output() .unwrap(); assert!( install.status.success(), "{}", String::from_utf8_lossy(&install.stderr) ); let output = Command::from_std(smith(&home)) .timeout(Duration::from_secs(20)) .env("OPENAI_API_KEY", "lifecycle-effect-secret") .args([ "eval", "--provider", "openai", "--model", "test-model", "--base-url", &format!("http://{address}/v1"), "raw-root", ]) .output() .unwrap(); assert!( output.status.success(), "{}", String::from_utf8_lossy(&output.stderr) ); let requests = worker.finish(); assert!( output.status.success(), "{}", String::from_utf8_lossy(&output.stderr) ); assert_eq!(requests.len(), 1); received .recv_timeout(Duration::from_secs(3)) .expect("lifecycle must perform real HTTP"); effect_worker.join().unwrap(); let path = std::fs::read_dir(home.join("data/smith/sessions/run")) .unwrap() .filter_map(Result::ok) .map(|entry| entry.path()) .find(|path| path.extension().is_some_and(|extension| extension == "smh")) .unwrap(); let session = smith_core::store::SessionReader::open(&path) .unwrap() .selected_session(smith::Config::default_valid().frame_bytes_max) .unwrap(); for entry in session.active_branch().entries() { let content = format!("{:?}", session.content(entry).unwrap()); assert!(!content.contains("lifecycle-effect-secret")); assert!( !content.contains("6c6966656379636c652d6566666563742d736563726574"), "hex must not bypass secret masking" ); } let root_id = session .active_branch() .entries() .iter() .find(|entry| { matches!( entry.kind, smith_core::session::EntryKind::Message { role: smith::message::Role::User, .. } ) }) .unwrap() .id; let mut invocation_id = None; let mut effect_id = None; let mut kinds = Vec::new(); for entry in session.active_branch().entries().iter() { let content = session.content(entry).unwrap(); assert!(!matches!( content, smith_core::session::EntryContent::ToolCall { .. } | smith_core::session::EntryContent::ToolResult { .. } )); if let smith_core::session::EntryContent::Meta { kind, detail } = content { if smith_core::metadata::lifecycle::reserved(&kind) { let detail = serde_json::to_value(detail).unwrap(); assert_eq!(detail["root_entry_id"], serde_json::json!(root_id)); assert!(detail.get("call_id").is_none()); if kind == smith_core::metadata::lifecycle::INVOCATION_INTENT { invocation_id = Some(entry.id); } assert_eq!( detail["invocation_id"], serde_json::json!(invocation_id.unwrap()) ); if kind == smith_core::metadata::lifecycle::EFFECT_INTENT { effect_id = Some(entry.id); } if kind.starts_with("lifecycle-effect-") { assert_eq!(detail["effect_id"], serde_json::json!(effect_id.unwrap())); assert_eq!(detail["ordinal"], 0); } } if kind.starts_with("lifecycle-") && kind != "lifecycle-decision" { kinds.push(kind); } } } assert_eq!( kinds, [ "lifecycle-invocation-intent", "lifecycle-effect-intent", "lifecycle-effect-completion", "lifecycle-invocation-completion" ] ); } fn install_settled_http(home: &Path, origin: &str) { install_boundary_http(home, origin, "Settled", false); } fn install_boundary_http(home: &Path, origin: &str, boundary: &str, actionable: bool) { let source = include_str!("../../smith-harness/tests/fixtures/lifecycle.rs") .replace("Boundary::TurnComplete", &format!("Boundary::{boundary}")) .replace("vec![Action::EnsureContinuation]", &format!(r#"{{ let response = smith::plugin::net_scope::request(&smith::plugin::net_scope::HttpRequest {{ destination: "{origin}".into(), method: "POST".into(), headers: vec![], body: b"{{}}".to_vec(), options: smith::plugin::net_scope::RequestOptions {{ timeout_ms: 10000, response_bytes_max: 256 }}, }})?; if response.status != 200 {{ return Err("HTTP failed".into()); }} vec![] }}"#)) .replace(" use exports::smith::plugin::lifecycle::*;\n Ok(if", " Ok(if"); let id = if actionable { "example:actionable-http" } else { "example:notification-http" }; let artifact = support::compile_fixture( &home.join(format!("guest-{boundary}")), &source, if actionable { "actionable-http" } else { "settled-http" }, ); let package = home.join(format!("http-package-{boundary}")); std::fs::create_dir_all(&package).unwrap(); std::fs::copy(artifact, package.join("fixture.wasm")).unwrap(); std::fs::write(package.join("plugin.kdl"), format!("id \"{id}\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"fixture.wasm\"\ncapability \"net-scope\"\n")).unwrap(); Command::from_std(smith(home)) .args([ "plugin", "install", package.to_str().unwrap(), "--grant", "net-scope", "--net-origin", origin, ]) .assert() .success(); } fn install_notification_composition(home: &Path, cancel: bool) { let source = include_str!("../../smith-harness/tests/fixtures/composition.rs") .replace("WIT_PATH", "wit") .replace( "return Ok(\"child complete\".into());", if cancel { "loop { core::hint::spin_loop(); }" } else { "return Ok(\"child complete\".into());" }, ); let artifact = support::compile_fixture( &home.join("composition-guest"), &source, "notification-composition", ); for id in ["a", "target"] { let package = home.join(format!("composition-{id}")); std::fs::create_dir_all(&package).unwrap(); std::fs::copy(&artifact, 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(); Command::from_std(smith(home)) .args([ "plugin", "install", package.to_str().unwrap(), "--grant", "workspace", ]) .assert() .success(); } std::fs::create_dir(home.join("A")).unwrap(); } // Observe the real atomic file effect, not a production readiness hook. // Register before startup; arm the deadline only when the provider is ready. #[cfg(target_os = "linux")] fn child_write_signal(directory: &Path) -> impl FnOnce() + use<> { use std::os::fd::{AsRawFd, FromRawFd}; use std::os::unix::ffi::OsStrExt; #[repr(C)] struct PollDescriptor { descriptor: i32, events: i16, returned: i16, } unsafe extern "C" { fn inotify_init1(flags: i32) -> i32; fn inotify_add_watch(descriptor: i32, path: *const std::ffi::c_char, mask: u32) -> i32; fn poll(descriptors: *mut PollDescriptor, count: usize, timeout: i32) -> i32; } // SAFETY: CLOEXEC produces an owned descriptor, checked before adoption. let descriptor = unsafe { inotify_init1(0x80000) }; assert!(descriptor >= 0); // SAFETY: successful init returned a fresh descriptor owned only here. let mut events = unsafe { std::fs::File::from_raw_fd(descriptor) }; let path = std::ffi::CString::new(directory.as_os_str().as_bytes()).unwrap(); // SAFETY: live descriptor and terminated path remain valid for this call. assert!(unsafe { inotify_add_watch(events.as_raw_fd(), path.as_ptr(), 0x80) } >= 0); move || { let mut descriptor = PollDescriptor { descriptor: events.as_raw_fd(), events: 1, returned: 0, }; // SAFETY: one initialized descriptor is writable for the duration of poll. assert_eq!( unsafe { poll(&raw mut descriptor, 1, 20000) }, 1, "child file-effect deadline" ); let mut bytes = [0; 4096]; let count = events.read(&mut bytes).unwrap(); assert!( bytes[..count] .windows(b"written.txt\0".len()) .any(|name| name == b"written.txt\0") ); } } #[cfg(target_os = "linux")] #[test] fn child_file_effect_signal_retains_events_before_wait() { let root = std::env::temp_dir().join(format!("smith-child-signal-{}", smith::SessionId::new())); std::fs::create_dir_all(&root).unwrap(); let wait = child_write_signal(&root); std::fs::write(root.join("pending"), b"evidence").unwrap(); std::fs::rename(root.join("pending"), root.join("written.txt")).unwrap(); wait(); std::fs::remove_dir_all(root).unwrap(); } #[test] fn rpc_settled_notifications_defer_both_delivery_modes_until_cleanup() { rpc_notification_queue(false, false, None); } #[test] fn rpc_settled_notifications_defer_after_turn_first_until_cleanup() { rpc_notification_queue_order(false, false, None, true); } #[test] fn rpc_cancelled_root_finishes_bounded_notifications_before_wait_and_queued_work() { rpc_notification_queue(true, false, None); } #[test] fn rpc_required_failure_retains_completed_write_and_notification_queue() { rpc_notification_queue(false, true, None); } #[test] fn rpc_cancel_decision_retains_completed_write_and_runs_bounded_notification_cleanup() { rpc_notification_queue(true, true, None); } #[cfg(target_os = "linux")] #[test] fn rpc_cancelled_structured_child_joins_before_notifications_and_queued_work() { rpc_notification_queue(true, false, Some("caller")); } #[test] fn rpc_parent_failure_retains_completed_child_write_and_notification_queue() { rpc_notification_queue(false, false, Some("parent-fail")); } fn rpc_notification_queue(cancel: bool, required_failure: bool, composition: Option<&str>) { rpc_notification_queue_order(cancel, required_failure, composition, false); } fn rpc_notification_queue_order( cancel: bool, required_failure: bool, composition: Option<&str>, after_first: bool, ) { rpc_notification_scenario(cancel, required_failure, composition, after_first, false); } #[test] fn rpc_abort_actionable_http_acknowledges_invocation_before_settlement() { rpc_notification_scenario(true, false, None, false, true); } fn rpc_notification_scenario( cancel: bool, required_failure: bool, composition: Option<&str>, after_first: bool, actionable_cancel: bool, ) { use std::process::Stdio; let home = std::env::temp_dir().join(format!("sln-{:x}", smith::SessionId::new().as_u128())); std::fs::create_dir_all(&home).unwrap(); let listener = TcpListener::bind("127.0.0.1:0").unwrap(); let origin = format!("http://{}", listener.local_addr().unwrap()); install_settled_http(&home, &origin); let actionable = actionable_cancel.then(|| { let listener = TcpListener::bind("127.0.0.1:0").unwrap(); install_boundary_http(&home, &format!("http://{}", listener.local_addr().unwrap()), "PreSettlement", true); let (ready_tx, ready_rx) = std::sync::mpsc::sync_channel(1); let (cancelled_tx, cancelled_rx) = std::sync::mpsc::sync_channel(1); let server_home = home.clone(); let worker = std::thread::spawn(move || { for index in 0..3 { let (mut stream, _) = listener.accept().unwrap(); stream.set_read_timeout(Some(Duration::from_secs(10))).unwrap(); assert_eq!(body(&mut stream), serde_json::json!({})); let session = smith_core::store::SessionReader::open(&session_path(&server_home)).unwrap() .selected_session(smith::Config::default_valid().frame_bytes_max).unwrap(); let last = session.active_branch().entries().last().unwrap(); assert!(matches!(session.content(last).unwrap(), smith_core::session::EntryContent::Meta { kind, .. } if kind == "lifecycle-effect-intent")); if index == 0 { ready_tx.send(()).unwrap(); let mut byte = [0]; assert_eq!(stream.read(&mut byte).expect("HTTP cancellation closes connection"), 0); cancelled_tx.send(()).unwrap(); } else { stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}").unwrap(); } } }); (ready_rx, cancelled_rx, worker) }); // A failed notification must not suppress the HTTP consumer; actions from a // distinct later consumer must not reopen provider work. install_boundary_decision( &home, "example:notification-fault", "return Err(\"private-notification-error\".into())", false, "Settled", ); install_boundary_decision( &home, "example:notification-reopen", "vec![Action::EnsureContinuation]", false, "Settled", ); const NOTIFICATION_COUNT: usize = 3; // All reports must fit while the client waits for settlement before draining them. let (ready_tx, ready_rx) = std::sync::mpsc::sync_channel(NOTIFICATION_COUNT); let (release_tx, release_rx) = std::sync::mpsc::sync_channel(1); let server_home = home.clone(); let effect_worker = std::thread::spawn(move || { for index in 0..NOTIFICATION_COUNT { let (mut stream, _) = listener.accept().unwrap(); assert_eq!(body(&mut stream), serde_json::json!({})); let path = session_path(&server_home); let session = smith_core::store::SessionReader::open(&path) .unwrap() .selected_session(smith::Config::default_valid().frame_bytes_max) .unwrap(); let kinds: Vec<_> = session .active_branch() .entries() .iter() .filter_map(|entry| match session.content(entry).unwrap() { smith_core::session::EntryContent::Meta { kind, .. } => Some(kind), _ => None, }) .collect(); assert_eq!( kinds.last().unwrap(), "lifecycle-effect-intent", "guest HTTP requires prior durable acknowledgement" ); ready_tx.send(index).unwrap(); if index == 0 { release_rx .recv_timeout(Duration::from_secs(10)) .expect("explicit notification release"); } stream .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}") .unwrap(); } }); if required_failure { install_decision( &home, "example:required", if cancel { "vec![Action::Cancel, Action::SetModel(\"unavailable\".into())]" } else { "vec![Action::SetModel(\"unavailable\".into())]" }, true, ); } if composition.is_some() { install_notification_composition(&home, cancel); } #[cfg(target_os = "linux")] let child_effect = (composition.is_some() && cancel).then(|| child_write_signal(&home.join("A"))); #[cfg(not(target_os = "linux"))] let child_effect: Option = None; let (address, provider_worker) = composition.map_or_else( || provider(3, required_failure), |mode| { provider_tool_script( if cancel { 3 } else { 4 }, Some(("fixture:a/fixture", &format!(r#"{{"mode":"{mode}"}}"#))), !cancel, ) }, ); let mut invocation = smith(&home); invocation.arg("rpc"); if composition.is_some() { invocation.args([ "--tool-policy", "fixture:target/fixture=hidden", "--tool-delegation", "fixture:a/fixture=fixture:target/fixture#A", ]); } let mut child = Child( invocation .env("OPENAI_API_KEY", "settled-fixture-secret") .args([ "--provider", "openai", "--model", "test-model", "--base-url", &format!("http://{address}/v1"), ]) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn() .unwrap(), ); let mut input = child.0.stdin.take().unwrap(); let stdout = child.0.stdout.take().unwrap(); let (sender, receiver) = std::sync::mpsc::sync_channel(32); let reader = std::thread::spawn(move || { for line in BufReader::new(stdout).lines() { if sender .send(serde_json::from_str::(&line.unwrap()).unwrap()) .is_err() { break; } } }); let mut lines = Vec::new(); let mut receive = |id: u64| { for _ in 0..32 { let line = receiver .recv_timeout(Duration::from_secs(20)) .expect("bounded RPC response"); let found = line["id"] == id; lines.push(line.clone()); if found { return line; } } panic!("response identity not found"); }; writeln!( input, "{{\"id\":1,\"method\":\"prompt\",\"params\":{{\"input\":\"raw-root\"}}}}" ) .unwrap(); input.flush().unwrap(); let root = receive(1)["receipt"]["submission_id"].clone(); if let Some(wait) = child_effect { provider_worker .first_request .recv_timeout(Duration::from_secs(20)) .expect("provider must receive the root request before child execution"); wait(); writeln!( input, "{{\"id\":9,\"method\":\"abort\",\"params\":{{\"id\":{root}}}}}" ) .unwrap(); input.flush().unwrap(); assert_eq!(receive(9)["aborting"], true); } let actionable_worker = if let Some((ready, cancelled, worker)) = actionable { ready.recv_timeout(Duration::from_secs(20)).unwrap(); writeln!( input, "{{\"id\":19,\"method\":\"abort\",\"params\":{{\"id\":{root}}}}}" ) .unwrap(); input.flush().unwrap(); assert_eq!(receive(19)["aborting"], true); cancelled .recv_timeout(Duration::from_secs(10)) .expect("actionable HTTP canceled"); Some(worker) } else { None }; let notification = ready_rx.recv_timeout(Duration::from_secs(20)); if actionable_cancel && notification.is_err() { let session = smith_core::store::SessionReader::open(&session_path(&home)) .unwrap() .selected_session(smith::Config::default_valid().frame_bytes_max) .unwrap(); let mut open = 0; for entry in session.active_branch().entries() { if let smith_core::session::EntryContent::Meta { kind, .. } = session.content(entry).unwrap() { match kind.as_str() { "lifecycle-invocation-intent" => open += 1, "lifecycle-invocation-completion" => open -= 1, _ => {} } } } assert_eq!( open, 0, "canceled actionable handler must acknowledge invocation completion before Cancelled" ); } assert_eq!(notification.unwrap(), 0); let (first_delivery, second_delivery) = if after_first { ("AfterTurn", "BetweenRequests") } else { ("BetweenRequests", "AfterTurn") }; writeln!(input, "{{\"id\":2,\"method\":\"prompt\",\"params\":{{\"input\":\"queued-between\",\"key\":\"between-key\",\"delivery\":\"{first_delivery}\"}}}}\n{{\"id\":3,\"method\":\"prompt\",\"params\":{{\"input\":\"queued-after\",\"key\":\"after-key\",\"delivery\":\"{second_delivery}\"}}}}\n{{\"id\":4,\"method\":\"prompt\",\"params\":{{\"input\":\"queued-between\",\"key\":\"between-key\",\"delivery\":\"{first_delivery}\"}}}}\n{{\"id\":5,\"method\":\"wait\"}}\n{{\"id\":6,\"method\":\"status\"}}\n{{\"id\":7,\"method\":\"inspect\",\"params\":{{\"id\":{root}}}}}").unwrap(); input.flush().unwrap(); let between = receive(2); assert_eq!( between["event"], "start", "notification-window prompt must be admitted, not BUSY: {between}" ); assert_eq!(between["receipt"]["status"], "Queued"); let after = receive(3); assert_eq!(after["receipt"]["status"], "Queued"); let duplicate = receive(4); assert_eq!(duplicate["duplicate"], true); assert_eq!(duplicate["receipt"], between["receipt"]); let status = receive(6); assert_eq!(status["settled"], false); assert_eq!(status["active_submission_id"], root); assert_eq!(receive(7)["receipt"]["status"], "Running"); if cancel && composition.is_none() && !actionable_cancel { writeln!( input, "{{\"id\":8,\"method\":\"abort\",\"params\":{{\"id\":{root}}}}}" ) .unwrap(); input.flush().unwrap(); assert_eq!(receive(8)["aborting"], true); } if required_failure { assert_eq!( std::fs::read_to_string(home.join("proof.txt")).unwrap(), "evidence" ); } // Ordered status+inspection responses are the barrier, not wall-clock waiting. assert!( !lines .iter() .any(|line| line["event"] == "end" || line["id"] == 5) ); release_tx.send(()).unwrap(); let mut ends = Vec::new(); let mut waited = false; for _ in 0..32 { let line = receiver.recv_timeout(Duration::from_secs(20)).unwrap(); if line["event"] == "end" { ends.push(line["id"].clone()); } if line["id"] == 5 { assert_eq!(line["settled"], true); waited = true; } lines.push(line); if ends.len() == 3 && waited { break; } } assert_eq!( ends, [ serde_json::json!(1), serde_json::json!(2), serde_json::json!(3) ] ); assert!(waited); let terminal = lines .iter() .find(|line| line["id"] == 1 && line["event"] == "end") .unwrap(); if cancel { assert_eq!(terminal["reason"], "Abort"); } else if required_failure || composition.is_some() { assert_eq!(terminal["error"]["code"], "TURN_FAILED"); } else { assert_eq!(terminal["reason"], "EndTurn"); } let wait_position = lines.iter().position(|line| line["id"] == 5).unwrap(); assert!( wait_position < lines .iter() .position(|line| line["id"] == 2 && line["event"] == "end") .unwrap() ); assert_eq!(ready_rx.recv_timeout(Duration::from_secs(5)).unwrap(), 1); assert_eq!(ready_rx.recv_timeout(Duration::from_secs(5)).unwrap(), 2); drop(input); assert!(matches!( receiver.recv_timeout(Duration::from_secs(10)), Err(std::sync::mpsc::RecvTimeoutError::Disconnected) )); assert!(child.0.wait().unwrap().success()); reader.join().unwrap(); effect_worker.join().unwrap(); if let Some(worker) = actionable_worker { worker.join().unwrap(); } let requests = provider_worker.finish(); let expected = if composition.is_some() && !cancel { vec!["raw-root", "raw-root", "queued-between", "queued-after"] } else { vec!["raw-root", "queued-between", "queued-after"] }; assert_eq!(requests.len(), expected.len()); for (request, expected) in requests.iter().zip(expected) { let last_user = request["messages"] .as_array() .unwrap() .iter() .rev() .find(|message| message["role"] == "user") .unwrap(); assert_eq!(last_user["content"], expected); } let session_reader = smith_core::store::SessionReader::open(&session_path(&home)).unwrap(); let limit = smith::Config::default_valid().frame_bytes_max; if composition.is_some() { assert_eq!( std::fs::read_to_string(home.join("A/written.txt")).unwrap(), "child committed" ); } let mut cancelled_effects = 0; let mut settlements = Vec::new(); let mut queue = Vec::new(); let mut trace = Vec::new(); let mut diagnostics = Vec::new(); session_reader .scan(limit, |frame| { match frame { smith_core::frame::Frame::Settlement { submission_id, reason, .. } => { if actionable_cancel && serde_json::json!(submission_id) == root { assert_eq!(reason, Some(smith::StopReason::Abort)); } settlements.push(serde_json::json!(submission_id)); trace.push("settlement".to_string()); } smith_core::frame::Frame::Known { entry } | smith_core::frame::Frame::NestedCommit { entry } => { if let smith_core::session::EntryContent::Meta { kind, detail } = smith_core::session::EntryContent::decode(&entry.content)? { if kind == smith_core::metadata::NOTIFICATION_QUEUE_KIND { let intent: smith_core::control::NotificationAdmission = detail.deserialize_as().unwrap(); queue.push(intent); } else if kind.starts_with("lifecycle-") || kind.starts_with("nested-effect-") { if actionable_cancel && kind == "lifecycle-effect-completion" { let value = serde_json::to_value(&detail).unwrap(); if value["root_entry_id"] == root && value["ok"] == false { assert!(!value["output"].as_str().unwrap().is_empty()); cancelled_effects += 1; } } trace.push(kind); } else if kind == "lifecycle_decision" { let value = serde_json::to_value(detail).unwrap(); if value[0]["boundary"] == "Settled" { diagnostics.push(value[1]["diagnostics"].clone()); } } } } _ => {} } Ok(()) }) .unwrap(); assert_eq!( settlements, [ root.clone(), between["receipt"]["submission_id"].clone(), after["receipt"]["submission_id"].clone() ] ); assert_eq!(queue.len(), 2); assert_eq!( queue[0].delivery, if after_first { smith_core::agent::Delivery::AfterTurn } else { smith_core::agent::Delivery::BetweenRequests } ); assert_eq!( queue[1].delivery, if after_first { smith_core::agent::Delivery::BetweenRequests } else { smith_core::agent::Delivery::AfterTurn } ); for intent in queue { assert_eq!(serde_json::json!(intent.notifying_root), root); } assert_eq!(diagnostics.len(), 3); if !cancel && !required_failure && composition.is_none() && !after_first { let rendered = diagnostics[0] .as_array() .unwrap() .iter() .map(|item| { format!( "{}:{}:{}", item["component"].as_str().unwrap(), item["handler"].as_str().unwrap(), item["code"].as_str().unwrap() ) }) .collect::>() .join("\n"); insta::assert_snapshot!(rendered, @r#" example:notification-fault:continue:HandlerFailed example:notification-reopen:continue:BatchRejected "#); } for diagnostic in diagnostics { assert!( !diagnostic.as_array().unwrap().iter().any(|item| { item["component"] == "example:notification-http" && item["code"] == "HandlerFailed" }), "every settled HTTP notification must succeed: {diagnostic}" ); assert_eq!( diagnostic .as_array() .unwrap() .iter() .filter(|item| item["code"] == "BatchRejected") .count(), 1 ); } // Every terminal is strictly after all three invocation completions. HTTP // completion is not handler return; both records are required before ack. let mut pending = 0; let mut pending_effects = 0; let mut lifecycle_effects = 0; let mut pending_children = 0; let mut nested_completions = 0; for kind in trace { match kind.as_str() { "nested-effect-intent" => pending_children += 1, "nested-effect-completion" => { pending_children -= 1; nested_completions += 1; } "lifecycle-invocation-intent" => { assert_eq!( pending_children, 0, "structured children join before notification" ); pending += 1; } "lifecycle-effect-intent" => pending_effects += 1, "lifecycle-effect-completion" => { pending_effects -= 1; lifecycle_effects += 1; } "lifecycle-invocation-completion" => { assert_eq!(pending_effects, 0); pending -= 1; } "settlement" => assert_eq!(pending, 0), _ => {} } } assert_eq!(pending, 0); assert_eq!(pending_effects, 0); if actionable_cancel { assert_eq!( cancelled_effects, 1, "canceled HTTP retains terminal effect evidence" ); assert_eq!( lifecycle_effects, 6, "three actionable effects, including cancellation, plus three notifications" ); } assert_eq!(pending_children, 0); if composition.is_some() { assert_eq!( nested_completions, 2, "one child plus its completed file effect" ); } drop(session_reader); std::fs::remove_dir_all(home).unwrap(); } fn session_path(home: &Path) -> std::path::PathBuf { std::fs::read_dir(home.join("data/smith/sessions/run")) .unwrap() .filter_map(Result::ok) .map(|entry| entry.path()) .find(|path| path.extension().is_some_and(|ext| ext == "smh")) .unwrap() } #[test] fn eval_does_not_exit_while_settled_http_notification_is_held() { use std::process::Stdio; let home = std::env::temp_dir().join(format!("slev-{:x}", smith::SessionId::new().as_u128())); std::fs::create_dir_all(&home).unwrap(); let listener = TcpListener::bind("127.0.0.1:0").unwrap(); install_settled_http(&home, &format!("http://{}", listener.local_addr().unwrap())); let (ready_tx, ready_rx) = std::sync::mpsc::sync_channel(1); let (release_tx, release_rx) = std::sync::mpsc::sync_channel(1); let effect_worker = std::thread::spawn(move || { let (mut stream, _) = listener.accept().unwrap(); assert_eq!(body(&mut stream), serde_json::json!({})); ready_tx.send(()).unwrap(); release_rx .recv_timeout(Duration::from_secs(10)) .expect("explicit eval notification release"); stream .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}") .unwrap(); }); let (address, provider_worker) = provider(1, false); let mut child = Child( smith(&home) .env("OPENAI_API_KEY", "settled-fixture-secret") .args([ "eval", "--provider", "openai", "--model", "test-model", "--base-url", &format!("http://{address}/v1"), "raw-root", ]) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn() .unwrap(), ); let mut stdout = child.0.stdout.take().unwrap(); let (done_tx, done_rx) = std::sync::mpsc::sync_channel(1); let reader = std::thread::spawn(move || { let mut output = Vec::new(); stdout.read_to_end(&mut output).unwrap(); let _sent = done_tx.send(output); }); ready_rx.recv_timeout(Duration::from_secs(20)).unwrap(); assert!( child.0.try_wait().unwrap().is_none(), "eval must retain joined notification work" ); assert!(matches!( done_rx.try_recv(), Err(std::sync::mpsc::TryRecvError::Empty) )); let path = session_path(&home); let session_reader = smith_core::store::SessionReader::open(&path).unwrap(); let limit = smith::Config::default_valid().frame_bytes_max; let mut settlements = 0; session_reader .scan(limit, |frame| { if matches!(frame, smith_core::frame::Frame::Settlement { .. }) { settlements += 1; } Ok(()) }) .unwrap(); assert_eq!( settlements, 0, "HTTP readiness precedes terminal persistence" ); release_tx.send(()).unwrap(); assert_eq!( done_rx.recv_timeout(Duration::from_secs(20)).unwrap(), b"first\n" ); assert!(child.0.wait().unwrap().success()); reader.join().unwrap(); effect_worker.join().unwrap(); assert_eq!(provider_worker.finish().len(), 1); }