cargo.build, the WASI test runner, the console, and core.notify ordering

- cargo.build (native): one source copy, tests under WASI, release
  build to /state/cargo/builds/<n>/, process groups killed on cancel.
  The host writes the SDK copy to /plugins/cargo/sdk/ at start.
- sausage test-runner: one instance per test, in parallel, with its own
  mount table (empty world, package folder at its path); answers
  core.lock, core.unlock, core.notify, core.log.
- The console: rustyline, notifications while typing, ls/cat through the
  process view, methods, ps, log, activate, revert, stop, kill.
- core.notify on stdout is handled in order, before the response.
This commit is contained in:
2026-10-23 01:42:19 +01:00
parent 0516158883
commit 8d058666dc
23 changed files with 1786 additions and 296 deletions
+274 -250
View File
@@ -1,69 +1,48 @@
//! Rust build helpers for the native cargo plugin.
use std::path::{Path, PathBuf};
use std::time::Instant;
use anyhow::{Context, Result, anyhow, bail};
use serde_json::Value;
//! Native cargo builds, one immutable source copy, SDK installation, and process-group cancellation.
use crate::{Host, world::World};
use anyhow::{Context, Result, bail};
use include_dir::{Dir, include_dir};
use serde_json::{Value, json};
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use tokio::process::Command;
use include_dir::{Dir, include_dir};
use crate::world::{self, World};
/// The name reserved for the native Rust build plugin.
/// The native build plugin.
pub const ID: &str = "cargo";
/// The WASI component target used for Rust plugin builds.
/// The target used for builds and tests.
pub const TARGET: &str = "wasm32-wasip2";
static SDK: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/../sdk/src");
static MACROS: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/../sdk/macros/src");
static INTERFACE: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/../interface/src");
/// The source of the `sausage-actor` SDK.
static SDK_SRC: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/../sdk/src");
static SDK_MACROS: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/../sdk/macros/src");
#[derive(Clone, Default, Debug)]
/// The totals and output from a source tree's test run.
#[derive(Clone, Default, Debug, serde::Serialize)]
/// Test totals and captured output.
pub struct TestResults {
/// The number of tests that passed.
pub passed: u64,
/// The number of tests that failed.
pub failed: u64,
/// The test output retained for the caller.
pub output: String,
}
/// The component artifact and diagnostics from a successful build.
pub struct BuildOutput {
/// Host path of the published `.wasm` file.
pub wasm: PathBuf,
/// The elapsed build time in seconds.
pub seconds: f64,
/// The compiler diagnostics without progress lines.
pub warnings: String,
/// The totals and output of the test phase.
pub tests: TestResults,
}
/// A successful build, usable without a running host.
#[derive(Debug)]
/// The failed build or test phase and its diagnostics.
pub struct BuildOutput {
pub wasm: PathBuf,
pub warnings: String,
pub tests: TestResults,
}
/// A failed compiler or test phase, with its complete diagnostics.
#[derive(Debug)]
pub struct BuildFailure {
/// The reason that the build could not complete.
pub reason: String,
/// The totals and output of the test phase.
pub tests: TestResults,
}
impl std::fmt::Display for BuildFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.tests.failed > 0 && !self.tests.output.is_empty() {
write!(f, "{}:\n{}", self.reason, self.tests.output)
} else {
f.write_str(&self.reason)
}
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "{}\n{}", self.reason, self.tests.output)
}
}
impl std::error::Error for BuildFailure {}
impl From<anyhow::Error> for BuildFailure {
fn from(error: anyhow::Error) -> Self {
Self {
@@ -72,170 +51,271 @@ impl From<anyhow::Error> for BuildFailure {
}
}
}
/// All builds share one target folder, so they run one at a time.
/// A host-independent builder. Each builder serializes access to its work and target folders.
pub struct Cargo {
world: World,
builds: tokio::sync::Mutex<()>,
runner: PathBuf,
}
impl Cargo {
/// Create the plugin and install the SDK of this host into `public/sdk/`.
/// Installs changed SDK files and selects the current sausage executable as runner.
pub fn new(world: World) -> Result<Self> {
let sdk = world.plugin_dir(ID).join("sdk");
write_dir(&SDK_SRC, &sdk.join("src"))?;
write_dir(&SDK_MACROS, &sdk.join("macros/src"))?;
world::write_atomic(
&sdk.join("Cargo.toml"),
include_bytes!("../../sdk/Cargo.toml"),
)?;
world::write_atomic(
&sdk.join("macros/Cargo.toml"),
include_bytes!("../../sdk/macros/Cargo.toml"),
)?;
Ok(Self { world })
}
/// Run native `cargo test` unless opted out, then build and publish the
/// crate's `wasm32-wasip2` binary under `/plugin/cargo/public/builds/<n>/`.
pub async fn build(
&self,
source: &Path,
skip_tests: bool,
) -> std::result::Result<BuildOutput, BuildFailure> {
if !source.join("Cargo.toml").is_file() {
return Err(BuildFailure {
reason: format!("{} has no Cargo.toml", source.display()),
tests: TestResults::default(),
});
let mut runner = std::env::current_exe()?;
if runner
.parent()
.is_some_and(|parent| parent.file_name().is_some_and(|name| name == "deps"))
{
runner = runner.parent().unwrap().parent().unwrap().join("sausage");
}
let started = Instant::now();
let data = self.world.state_dir(ID);
let build_dir = data.join("work");
world::replace_source(source, &build_dir)?;
let manifest = build_dir.join("Cargo.toml");
let original = std::fs::read_to_string(&manifest).context("read build manifest")?;
let prepared = prepare_manifest(&original, Some(&self.world.plugin_dir(ID).join("sdk")))?;
std::fs::write(&manifest, prepared).context("write prepared manifest")?;
let target_dir = data.join("target");
let tests = if skip_tests {
TestResults {
output: "skipped (skip_tests=true)".into(),
..TestResults::default()
}
} else {
match run_tests(&build_dir, &target_dir).await {
Ok(tests) => tests,
Err(error) => {
let output = format!("cannot run cargo tests: {error:#}");
return Err(BuildFailure {
reason: output.clone(),
tests: TestResults {
failed: 1,
output,
..TestResults::default()
},
});
Self::with_runner(world, runner)
}
/// Installs the SDK and accepts an explicit sausage binary, including for init integrations.
pub fn with_runner(world: World, runner: PathBuf) -> Result<Self> {
let sdk = world.plugin_dir(ID).join("sdk");
write_dir(&SDK, &sdk.join("src"))?;
write_dir(&MACROS, &sdk.join("macros/src"))?;
write_dir(&INTERFACE, &sdk.join("interface/src"))?;
for (manifest, destination, interface_path) in [
(
include_str!("../../sdk/Cargo.toml"),
sdk.join("Cargo.toml"),
Some("interface"),
),
(
include_str!("../../sdk/macros/Cargo.toml"),
sdk.join("macros/Cargo.toml"),
Some("../interface"),
),
(
include_str!("../../interface/Cargo.toml"),
sdk.join("interface/Cargo.toml"),
None,
),
] {
let mut document: toml_edit::DocumentMut = manifest.parse()?;
document["workspace"] = toml_edit::table();
if let Some(path) = interface_path {
for section in ["dependencies", "dev-dependencies"] {
if let Some(dependency) = document
.get_mut(section)
.and_then(|table| table.get_mut("sausage-interface"))
{
dependency["path"] = toml_edit::value(path);
}
}
}
write_changed(&destination, document.to_string().as_bytes())?;
}
Ok(Self {
world,
runner,
builds: tokio::sync::Mutex::new(()),
})
}
/// Registers cargo.build as a cancellable native process.
pub fn register(self, host: &Host) {
let builder = Arc::new(self);
host.register_native(ID,Arc::new(move |_context,method,params| {
let builder=builder.clone();
Box::pin(async move {
if method!="build" { return Err(crate::error(-32601,"cargo method not found")); }
let plugin=params["plugin"].as_str().ok_or_else(||crate::error(-32602,"plugin must be a string"))?;
match builder.build(&builder.world.plugin_dir(plugin).join("source")).await {
Ok(output)=>Ok(json!({"wasm":format!("/{}",output.wasm.strip_prefix(&builder.world.root).unwrap().display()),"tests":output.tests,"warnings":output.warnings})),
Err(failure)=>Err(json!({"code":-32004,"message":failure.reason,"data":{"tests":failure.tests}})),
}
})
}));
}
/// Copies source once, runs WASI tests, then publishes a release component under state/cargo/builds.
pub async fn build(&self, source: &Path) -> std::result::Result<BuildOutput, BuildFailure> {
let _build = self.builds.lock().await;
let work = self.world.state_dir(ID).join("work");
let target = self.world.state_dir(ID).join("target");
let source = source.to_path_buf();
let copy = work.clone();
let sdk = self.world.plugin_dir(ID).join("sdk");
tokio::task::spawn_blocking(move || -> Result<()> {
if copy.exists() {
std::fs::remove_dir_all(&copy)?;
}
copy_source(&source, &copy)?;
let manifest = copy.join("Cargo.toml");
let text = std::fs::read_to_string(&manifest)?;
std::fs::write(manifest, prepare_manifest(&text, &sdk)?)?;
Ok(())
})
.await
.map_err(anyhow::Error::from)??;
let mut test = Command::new("cargo");
test.args(["test", "--offline", "--target", TARGET, "--target-dir"])
.arg(&target)
.current_dir(&work)
.env(
"CARGO_TARGET_WASM32_WASIP2_RUNNER",
format!("{} test-runner", self.runner.display()),
);
let output = run_command(test).await?;
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
let tests = TestResults {
passed: test_count(&stdout, "passed"),
failed: if output.status.success() {
0
} else {
test_count(&stdout, "failed").max(1)
},
output: format!("{stdout}\n{stderr}"),
};
if tests.failed > 0 {
if !output.status.success() {
return Err(BuildFailure {
reason: "unit tests failed".into(),
reason: "WASI tests failed".into(),
tests,
});
}
let build_result: Result<BuildOutput> = async {
let mut cmd = Command::new("cargo");
cmd.arg("build")
.arg("--release")
.arg("--target")
.arg(TARGET)
.arg("--message-format=json-render-diagnostics")
.arg("--target-dir")
.arg(&target_dir)
.current_dir(&build_dir)
.kill_on_drop(true);
let out = cmd
.output()
.await
.context("cannot run cargo (is it on PATH, with the wasm32-wasip2 target?)")?;
let stderr = String::from_utf8_lossy(&out.stderr).to_string();
if !out.status.success() {
bail!("{}", strip_progress(&stderr));
}
let result = async {
let mut build = Command::new("cargo");
build
.args([
"build",
"--offline",
"--release",
"--target",
TARGET,
"--message-format=json-render-diagnostics",
"--target-dir",
])
.arg(&target)
.current_dir(&work);
let output = run_command(build).await?;
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
let mut diagnostics = stderr.to_string();
let mut artifact = None;
for line in String::from_utf8_lossy(&out.stdout).lines() {
if let Ok(v) = serde_json::from_str::<Value>(line)
&& v["reason"] == "compiler-artifact"
&& let Some(exe) = v["executable"].as_str()
{
artifact = Some(PathBuf::from(exe));
for line in stdout.lines() {
if let Ok(message) = serde_json::from_str::<Value>(line) {
if message["reason"] == "compiler-artifact"
&& let Some(executable) = message["executable"].as_str()
{
artifact = Some(PathBuf::from(executable));
}
if let Some(rendered) = message["message"]["rendered"].as_str() {
diagnostics.push_str(rendered);
}
}
}
let artifact = artifact.ok_or_else(|| {
anyhow!("cargo built no binary. Does the crate have a src/main.rs?")
})?;
let n = next_build_number(&self.world.plugin_dir(ID).join("builds"))?;
let dest_dir = self.world.plugin_dir(ID).join("builds").join(n.to_string());
let name = artifact.file_name().context("artifact has no file name")?;
let dest = dest_dir.join(name);
world::write_atomic(&dest, &std::fs::read(&artifact)?)?;
if !output.status.success() {
bail!("{diagnostics}");
}
let artifact = artifact.context("cargo built no binary")?;
let builds = self.world.state_dir(ID).join("builds");
std::fs::create_dir_all(&builds)?;
let number = std::fs::read_dir(&builds)?
.filter_map(|entry| entry.ok()?.file_name().to_str()?.parse::<u64>().ok())
.max()
.unwrap_or(0)
+ 1;
let destination = builds
.join(number.to_string())
.join(artifact.file_name().unwrap());
crate::world::write_atomic(&destination, &tokio::fs::read(artifact).await?)?;
Ok(BuildOutput {
wasm: dest,
seconds: started.elapsed().as_secs_f64(),
warnings: strip_progress(&stderr),
wasm: destination,
warnings: strip_progress(&diagnostics),
tests: tests.clone(),
})
}
.await;
build_result.map_err(|error| BuildFailure {
result.map_err(|error: anyhow::Error| BuildFailure {
reason: format!("{error:#}"),
tests,
})
}
}
/// Run tests for the host triple: WASI crates' pure-logic tests can use the
/// normal Rust test harness without needing a WASI test runner.
async fn run_tests(build_dir: &Path, target_dir: &Path) -> Result<TestResults> {
let mut cmd = Command::new("cargo");
cmd.arg("test")
.arg("--target-dir")
.arg(target_dir)
.current_dir(build_dir)
.kill_on_drop(true);
let out = cmd.output().await.context("cannot run cargo test")?;
let stdout = String::from_utf8_lossy(&out.stdout).to_string();
let stderr = String::from_utf8_lossy(&out.stderr).to_string();
let combined = match (stdout.is_empty(), stderr.is_empty()) {
(true, true) => String::new(),
(false, true) => stdout,
(true, false) => stderr,
(false, false) => format!("{stdout}\n{stderr}"),
};
let output = strip_progress(&combined);
let passed = test_count(&output, "passed");
let mut failed = test_count(&output, "failed");
// Compilation failures and runner errors have no `test result` summary,
// but still make the default test phase fail.
if !out.status.success() && failed == 0 {
failed = 1;
/// Kills cargo and every descendant, including tests and build scripts, on cancellation.
struct ProcessGroup(u32);
impl Drop for ProcessGroup {
fn drop(&mut self) {
let _ = nix::sys::signal::killpg(
nix::unistd::Pid::from_raw(self.0 as i32),
nix::sys::signal::Signal::SIGKILL,
);
}
Ok(TestResults {
passed,
failed,
output,
})
}
/// Count the `passed` or `failed` totals in cargo's per-target test summaries.
/// Starts one process group and retains its guard across all output collection.
async fn run_command(mut command: Command) -> Result<std::process::Output> {
command
.process_group(0)
.kill_on_drop(true)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped());
let child = command.spawn().context("start cargo")?;
let _group = ProcessGroup(child.id().unwrap());
Ok(child.wait_with_output().await?)
}
/// Copies only source, excluding build outputs and git internals at every level.
fn copy_source(source: &Path, destination: &Path) -> Result<()> {
std::fs::create_dir_all(destination)?;
for entry in std::fs::read_dir(source)? {
let entry = entry?;
if matches!(entry.file_name().to_str(), Some("target" | ".git")) {
continue;
}
if entry.file_type()?.is_dir() {
copy_source(&entry.path(), &destination.join(entry.file_name()))?;
} else {
std::fs::copy(entry.path(), destination.join(entry.file_name()))?;
}
}
Ok(())
}
/// Repoints SDK dependencies without discarding feature flags and detaches the copied crate's workspace.
fn prepare_manifest(text: &str, sdk: &Path) -> Result<String> {
let mut document: toml_edit::DocumentMut = text.parse()?;
for section in ["dependencies", "dev-dependencies", "build-dependencies"] {
if let Some(dependency) = document
.get_mut(section)
.and_then(|table| table.get_mut("sausage-plugin"))
{
let features = dependency.get("features").cloned();
let defaults = dependency.get("default-features").cloned();
let mut replacement = toml_edit::InlineTable::new();
replacement.insert("path", sdk.display().to_string().into());
if let Some(features) = features.and_then(|value| value.as_value().cloned()) {
replacement.insert("features", features);
}
if let Some(defaults) = defaults.and_then(|value| value.as_value().cloned()) {
replacement.insert("default-features", defaults);
}
*dependency = toml_edit::value(replacement);
}
}
document["workspace"] = toml_edit::table();
Ok(document.to_string())
}
/// Writes embedded sources only when their bytes changed.
fn write_changed(path: &Path, bytes: &[u8]) -> Result<()> {
if std::fs::read(path).ok().as_deref() != Some(bytes) {
std::fs::create_dir_all(path.parent().unwrap())?;
std::fs::write(path, bytes)?;
}
Ok(())
}
/// Installs one embedded source tree without touching unchanged files.
fn write_dir(directory: &Dir<'_>, destination: &Path) -> Result<()> {
for file in directory.files() {
write_changed(&destination.join(file.path()), file.contents())?;
}
for directory in directory.dirs() {
write_dir(directory, destination)?;
}
Ok(())
}
/// Counts only final libtest summaries, never individual status lines.
fn test_count(output: &str, field: &str) -> u64 {
output
.lines()
.filter_map(|line| line.split_once("test result:").map(|(_, rest)| rest))
.filter_map(|line| line.strip_prefix("test result:"))
.flat_map(|summary| summary.split(';'))
.filter_map(|part| {
let words: Vec<_> = part.split_whitespace().collect();
@@ -244,77 +324,21 @@ fn test_count(output: &str, field: &str) -> u64 {
})
.sum()
}
/// Point a `sausage-actor` dependency at `sdk` (keeping its features), and
/// add an empty `[workspace]`, so the crate never joins a parent workspace.
fn prepare_manifest(text: &str, sdk: Option<&Path>) -> Result<String> {
let mut doc: toml_edit::DocumentMut = text.parse()?;
if let Some(sdk) = sdk {
for section in ["dependencies", "dev-dependencies", "build-dependencies"] {
let Some(dep) = doc
.get_mut(section)
.and_then(|t| t.get_mut("sausage-actor"))
else {
continue;
};
let mut table = dep.as_inline_table().cloned().unwrap_or_default();
for key in ["version", "path", "git", "branch", "tag", "rev", "registry"] {
table.remove(key);
}
table.insert("path", sdk.display().to_string().into());
*dep = toml_edit::value(table);
}
}
if doc.get("workspace").is_none() {
doc["workspace"] = toml_edit::table();
}
Ok(doc.to_string())
}
/// Write the files of `dir` below `to`. Skip files that did not change, so
/// that cargo does not rebuild the SDK at every start.
fn write_dir(dir: &Dir<'_>, to: &Path) -> Result<()> {
for file in dir.files() {
let dest = to.join(file.path());
let contents = match file.path().file_name() {
Some(n) if n == "Cargo.toml" => {
prepare_manifest(file.contents_utf8().context("Cargo.toml")?, None)?.into_bytes()
}
_ => file.contents().to_vec(),
};
if std::fs::read(&dest).ok() != Some(contents.clone()) {
std::fs::create_dir_all(dest.parent().context("no parent")?)?;
std::fs::write(&dest, contents)?;
}
}
for sub in dir.dirs() {
write_dir(sub, to)?;
}
Ok(())
}
fn next_build_number(dir: &Path) -> Result<u64> {
std::fs::create_dir_all(dir)?;
let max = std::fs::read_dir(dir)?
.filter_map(|e| e.ok()?.file_name().to_str()?.parse::<u64>().ok())
.max()
.unwrap_or(0);
Ok(max + 1)
}
/// Remove cargo's "Compiling ..." progress lines. Keep diagnostics.
fn strip_progress(stderr: &str) -> String {
stderr
/// Removes progress lines while retaining compiler diagnostics.
fn strip_progress(output: &str) -> String {
output
.lines()
.filter(|l| {
let t = l.trim_start();
!(t.starts_with("Compiling ")
|| t.starts_with("Downloaded ")
|| t.starts_with("Downloading ")
|| t.starts_with("Updating ")
|| t.starts_with("Locking ")
|| t.starts_with("Adding ")
|| t.starts_with("Blocking "))
.filter(|line| {
![
"Compiling ",
"Finished ",
"Blocking ",
"Updating ",
"Locking ",
"Adding ",
]
.iter()
.any(|prefix| line.trim_start().starts_with(prefix))
})
.collect::<Vec<_>>()
.join("\n")
+320 -19
View File
@@ -1,24 +1,325 @@
//! The minimal console reads explicit JSON-RPC calls and the quit command.
//! Console commands, readable results, and line editing with live notifications.
use crate::Host;
use tokio::io::{AsyncBufReadExt, BufReader};
use anyhow::{Result, bail};
use rustyline::{DefaultEditor, ExternalPrinter, error::ReadlineError};
use sausage_vfs::{Layer, Open};
use serde_json::{Value, json};
use std::time::{SystemTime, UNIX_EPOCH};
/// Read console lines until EOF or quit, and print each call's result.
pub async fn run_input(host: Host) -> anyhow::Result<()> {
let mut lines = BufReader::new(tokio::io::stdin()).lines();
while let Some(line) = lines.next_line().await? {
if line == "quit" {
break;
}
let (method, params) = line.split_once(' ').unwrap_or((&line, "{}"));
match serde_json::from_str(params) {
Ok(params) => {
let value = match host.call(method, params).await {
Ok(value) | Err(value) => value,
};
println!("{value}");
}
Err(error) => eprintln!("{error}"),
const HELP: &str = "text chat with the agent
plugin.method {json} call a plugin
ls [path] list a folder (default /)
cat <path> read a file
methods <plugin> show methods, params, types, and docs
ps show pid, plugin, method, parent, state, age
log [plugin or pid] [-n N] read the journal
activate <plugin> <wasm> activate a version
revert <plugin> restore the previous version
stop <pid> request a graceful stop
kill <pid> kill a process
help show this help
quit leave the console";
async fn read(host: &Host, path: &str) -> Result<Vec<u8>> {
let fs = host.filesystem().await?;
let file = fs
.open(
path,
Open {
follow: true,
..Open::default()
},
)
.await?;
let mut bytes = Vec::new();
loop {
let chunk = file.read(bytes.len() as u64, 65536).await?;
if chunk.is_empty() {
return Ok(bytes);
}
bytes.extend(chunk);
}
Ok(())
}
fn pretty(value: &Value) -> Result<String> {
Ok(serde_json::to_string_pretty(value)?)
}
async fn call(host: &Host, method: &str, params: Value, chat: bool) -> Result<String> {
match host.call(method, params).await {
Ok(value) if chat => Ok(value["text"]
.as_str()
.or_else(|| value.as_str())
.map(str::to_owned)
.unwrap_or(pretty(&value)?)),
Ok(value) => pretty(&value),
Err(error) => Ok(format!("error: {}", pretty(&error)?)),
}
}
/// Execute one input line without terminal IO. The caller handles quit after printing.
pub async fn execute_line(host: &Host, line: &str) -> Result<String> {
let line = line.trim();
if line.is_empty() {
return Ok(String::new());
}
let (command, rest) = line.split_once(char::is_whitespace).unwrap_or((line, ""));
let rest = rest.trim();
let words: Vec<_> = rest.split_whitespace().collect();
match command {
"help" if words.is_empty() => Ok(HELP.into()),
"quit" if words.is_empty() => Ok("Goodbye.".into()),
"ls" if words.len() <= 1 => {
let fs = host.filesystem().await?;
let mut entries = fs.list(words.first().copied().unwrap_or("/")).await?;
entries.sort_by(|a, b| a.name.cmp(&b.name));
Ok(entries
.into_iter()
.map(|e| format!("{}{}", e.name, if e.directory { "/" } else { "" }))
.collect::<Vec<_>>()
.join("\n"))
}
"cat" if !rest.is_empty() => Ok(String::from_utf8(read(host, rest).await?)?),
"methods" if words.len() == 1 => {
host.refresh_graph()
.await
.map_err(|error| anyhow::anyhow!("{error}"))?;
let document: Value = serde_json::from_slice(
&read(
host,
&format!("/proc/core/plugins/{}/discover.json", words[0]),
)
.await?,
)?;
let mut lines = Vec::new();
for method in document["methods"]
.as_array()
.ok_or_else(|| anyhow::anyhow!("discovery document has no methods"))?
{
let params = method["params"]
.as_array()
.map(|params| {
params
.iter()
.map(|p| {
format!(
"{}: {}",
p["name"].as_str().unwrap_or("?"),
p["schema"]["type"].as_str().unwrap_or("any")
)
})
.collect::<Vec<_>>()
.join(", ")
})
.unwrap_or_default();
lines.push(format!(
"{}.{}({}) -> {}",
words[0],
method["name"].as_str().unwrap_or("?"),
params,
method["result"]["schema"]["type"].as_str().unwrap_or("any")
));
for field in ["summary", "description"] {
if let Some(doc) = method[field].as_str() {
lines.push(format!(" {doc}"));
}
}
if let Some(params) = method["params"].as_array() {
for p in params {
if let Some(doc) = p["description"].as_str() {
lines.push(format!(" {}: {doc}", p["name"].as_str().unwrap_or("?")));
}
}
}
}
Ok(lines.join("\n"))
}
"ps" if words.is_empty() => {
let now = SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis();
let mut processes: Vec<_> = host.processes().into_iter().collect();
processes.sort_by(|a, b| a.0.cmp(&b.0));
let mut lines = vec!["PID PLUGIN METHOD PARENT STATE AGE".into()];
lines.extend(processes.into_iter().map(|(pid, p)| {
format!(
"{} {} {} {} {} {}s",
pid,
p.plugin,
p.method,
p.parent.as_deref().unwrap_or("-"),
p.state,
now.saturating_sub(p.start_time) / 1000
)
}));
Ok(lines.join("\n"))
}
"log" => {
let mut params = json!({});
let mut i = 0;
while i < words.len() {
if words[i] == "-n" {
i += 1;
let count: u64 = words
.get(i)
.ok_or_else(|| anyhow::anyhow!("log -n requires a count"))?
.parse()?;
params["limit"] = json!(count);
} else if params.get("pid").is_none() && params.get("plugin").is_none() {
let key = if words[i].len() > 36
&& words[i]
.get(words[i].len() - 36..)
.is_some_and(|suffix| uuid::Uuid::try_parse(suffix).is_ok())
{
"pid"
} else {
"plugin"
};
params[key] = json!(words[i]);
} else {
bail!("usage: log [plugin or pid] [-n N]");
}
i += 1;
}
call(host, "core.log", params, false).await
}
"activate" if words.len() == 2 => {
call(
host,
"core.activate",
json!({"plugin":words[0],"wasm":words[1]}),
false,
)
.await
}
"revert" if words.len() == 1 => {
call(host, "core.revert", json!({"plugin":words[0]}), false).await
}
"stop" | "kill" if words.len() == 1 => {
call(
host,
&format!("core.{command}"),
json!({"pid":words[0]}),
false,
)
.await
}
"help" | "quit" | "ls" | "methods" | "ps" | "activate" | "revert" | "stop" | "kill"
| "cat" => bail!("invalid arguments for {command}; use help"),
_ if command
.split_once('.')
.is_some_and(|(plugin, method)| !plugin.is_empty() && !method.is_empty()) =>
{
call(
host,
command,
if rest.is_empty() {
json!({})
} else {
serde_json::from_str(rest)?
},
false,
)
.await
}
_ => call(host, "agent.chat", json!({"text":line}), true).await,
}
}
/// Format text notifications dimly, while preserving other notification payloads.
pub fn notification_text(notification: &Value) -> String {
let params = &notification["params"];
if params["name"] == "text" {
let payload = &params["params"];
let text = payload["text"]
.as_str()
.or_else(|| payload.as_str())
.map(str::to_owned)
.unwrap_or_else(|| payload.to_string());
format!("\x1b[2m{text}\x1b[0m")
} else {
notification.to_string()
}
}
fn indent(text: &str) -> String {
text.lines()
.map(|line| format!(" {line}"))
.collect::<Vec<_>>()
.join("\n")
}
/// Run the line editor on a dedicated thread, with notifications through its external printer.
pub async fn run_input(host: Host) -> Result<()> {
let history = host.0.world.state_dir("core").join("history");
let (ready, started) = tokio::sync::oneshot::channel();
let (sender, mut lines) = tokio::sync::mpsc::unbounded_channel::<
Result<(String, tokio::sync::oneshot::Sender<bool>)>,
>();
// A detached terminal thread does not hold Tokio shutdown open at a blocked readline.
std::thread::spawn(move || {
let setup = (|| -> Result<_> {
let mut editor = DefaultEditor::new()?;
let _ = editor.load_history(&history);
let printer = editor.create_external_printer()?;
Ok((editor, printer))
})();
let (mut editor, printer) = match setup {
Ok(value) => value,
Err(error) => {
let _ = ready.send(Err(error));
return;
}
};
if ready.send(Ok(printer)).is_err() {
return;
}
let result = (|| -> Result<()> {
loop {
match editor.readline("> ") {
Ok(line) => {
editor.add_history_entry(&line)?;
editor.save_history(&history)?;
let (done, wait) = tokio::sync::oneshot::channel();
if sender.send(Ok((line, done))).is_err()
|| !wait.blocking_recv().unwrap_or(false)
{
break;
}
}
Err(ReadlineError::Interrupted) => break,
Err(ReadlineError::Eof) => break,
Err(error) => return Err(error.into()),
}
}
Ok(())
})();
if let Err(error) = result {
let _ = sender.send(Err(error));
}
});
let mut printer = started.await??;
let mut notifications = host.subscribe();
let print_notifications = async move {
loop {
match notifications.recv().await {
Ok(value) => printer.print(format!("{}\n", indent(&notification_text(&value))))?,
Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
Err(tokio::sync::broadcast::error::RecvError::Closed) => return Ok(()),
}
}
};
let commands = async {
while let Some(line) = lines.recv().await {
let (line, done) = line?;
let text = execute_line(&host, &line)
.await
.unwrap_or_else(|error| format!("error: {error}"));
if !text.is_empty() {
println!("{}", indent(&text));
}
let quit = line.trim() == "quit";
let _ = done.send(!quit);
if quit {
break;
}
}
Ok(())
};
tokio::select! {result=commands=>result,result=print_notifications=>result}
}
+4
View File
@@ -82,6 +82,10 @@ impl Host {
watcher: Mutex::new(None),
retirements: Mutex::new(std::collections::HashSet::new()),
}));
let cargo_world = host.0.world.clone();
let cargo =
tokio::task::spawn_blocking(move || crate::cargo::Cargo::new(cargo_world)).await??;
cargo.register(&host);
host.boot().await?;
Ok(host)
}
+27 -7
View File
@@ -4,6 +4,7 @@ use clap::{Parser, Subcommand};
use sausage::{Host, world::World};
use serde_json::json;
use std::{io::Write, path::PathBuf};
mod test_runner;
#[derive(Parser)]
#[command(version)]
struct Cli {
@@ -16,8 +17,18 @@ struct Cli {
}
#[derive(Subcommand)]
enum Cmd {
Init { dir: PathBuf },
Run { dir: PathBuf },
Init {
dir: PathBuf,
},
Run {
dir: PathBuf,
},
/// Run a WASI libtest component in isolated instances.
TestRunner {
wasm: PathBuf,
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
args: Vec<String>,
},
}
fn setting(var: &str, question: &str, default: Option<&str>) -> Result<String> {
if let Ok(v) = std::env::var(var)
@@ -41,6 +52,11 @@ fn setting(var: &str, question: &str, default: Option<&str>) -> Result<String> {
async fn main() -> Result<()> {
let cli = Cli::parse();
match cli.command {
Cmd::TestRunner { wasm, args } => {
if !test_runner::run(&wasm, args).await? {
std::process::exit(1);
}
}
Cmd::Init { dir } => {
let api = setting("SAUSAGE_API", "API shape", Some("openai-responses"))?;
if api != "openai-responses" && api != "anthropic-messages" {
@@ -65,10 +81,12 @@ async fn main() -> Result<()> {
None => Host::start(&dir).await?,
};
let mut notifications = host.subscribe();
let notification_task = tokio::spawn(async move {
while let Ok(notification) = notifications.recv().await {
println!("{notification}");
}
let notification_task = cli.headless.then(|| {
tokio::spawn(async move {
while let Ok(notification) = notifications.recv().await {
println!("{notification}");
}
})
});
let console = sausage::console::run_input(host.clone());
let exit = tokio::select! {
@@ -82,7 +100,9 @@ async fn main() -> Result<()> {
};
let exit = exit || host.restart_requested();
host.stop().await;
notification_task.abort();
if let Some(task) = notification_task {
task.abort();
}
if exit {
std::process::exit(75);
}
+24
View File
@@ -220,6 +220,30 @@ impl Host {
return;
}
if let Some(method) = v["method"].as_str() {
if method == "core.notify" {
// Deliver progress before a following response removes its destination.
let params = v.get("params").cloned().unwrap_or(json!({}));
let result = if !params.is_object() {
Err(error(-32602, "params must be an object"))
} else if let Some(name) = params["name"].as_str() {
self.notify(
pid,
name,
params.get("params").cloned().unwrap_or(json!({})),
);
Ok(Value::Null)
} else {
Err(error(-32602, "name must be a string"))
};
if let Some(id) = v.get("id") {
let response = match result {
Ok(result) => json!({"jsonrpc":"2.0", "id":id, "result":result}),
Err(error) => json!({"jsonrpc":"2.0", "id":id, "error":error}),
};
self.send(pid, response);
}
return;
}
let h = self.clone();
let caller = pid.to_string();
let method = method.to_string();
+11
View File
@@ -243,12 +243,23 @@ impl Runtime {
self: &Arc<Self>,
component: &Component,
cfg: InstanceConfig,
) -> Result<Instance> {
self.start_with_args(component, cfg, &[]).await
}
/// Start a WASI task with explicit CLI arguments, including its program name.
pub async fn start_with_args(
self: &Arc<Self>,
component: &Component,
cfg: InstanceConfig,
args: &[String],
) -> Result<Instance> {
let (host_in, guest_in) = tokio::io::duplex(64 * 1024);
let (guest_out, host_out) = tokio::io::duplex(64 * 1024);
let (guest_err, host_err) = tokio::io::duplex(64 * 1024);
let mut b = WasiCtx::builder();
b.args(args);
if cfg.network {
b.inherit_network()
.allow_tcp(true)
+237
View File
@@ -0,0 +1,237 @@
//! Cargo's WASI test runner: one compiled component, isolated instances, and a small core transport.
use anyhow::{Result, bail};
use sausage::{
Host,
runtime::{Exit, InstanceConfig, Runtime},
world::World,
};
use serde_json::{Value, json};
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use tokio::io::AsyncWriteExt;
use wasmtime::component::Component;
/// A temporary world remains alive through the end of its WASI store.
struct TestWorld(PathBuf);
impl Drop for TestWorld {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
struct Run {
exit: Exit,
output: String,
}
/// Runs one instance and separates JSON-RPC requests from ordinary test output.
async fn instance(
runtime: Arc<Runtime>,
component: Component,
args: Vec<String>,
package: PathBuf,
) -> Result<Run> {
let path = std::env::temp_dir().join(format!("sausage-test-{}", uuid::Uuid::now_v7()));
World::create(&path)?;
let world = TestWorld(path);
let host = Host::start(&world.0).await?;
let tracker = sausage_vfs::Process::default();
let mut filesystem = host.filesystem().await?;
filesystem.mount(
package.to_str().unwrap(),
Arc::new(sausage_vfs::Host::new(&package, tracker.clone())?),
false,
);
let mut env: Vec<_> = std::env::vars().filter(|(key, _)| key != "PWD").collect();
env.extend([
("PWD".into(), package.display().to_string()),
("SAUSAGE_PID".into(), "test".into()),
("SAUSAGE_PLUGIN".into(), "test".into()),
("SAUSAGE_PARENT".into(), "core".into()),
]);
let mut guest = runtime
.start_with_args(
&component,
InstanceConfig {
filesystem,
store_ended: tokio::sync::watch::channel(true).0,
network: true,
env,
},
&args,
)
.await?;
let mut stdin = guest.stdin.take().unwrap();
let mut stdout = std::mem::replace(&mut guest.stdout, tokio::sync::mpsc::channel(1).1);
let mut stderr = std::mem::replace(&mut guest.stderr, tokio::sync::mpsc::channel(1).1);
let mut exit = None;
let mut output = String::new();
let wait = guest.wait();
tokio::pin!(wait);
let mut stdout_open = true;
let mut stderr_open = true;
let mut handlers = tokio::task::JoinSet::new();
while exit.is_none() || stdout_open || stderr_open {
tokio::select! {
result=&mut wait, if exit.is_none()=>exit=Some(result),
line=stdout.recv(), if stdout_open=>match line {
Some(line)=> {
// Libtest can prefix its status line before SDK output. The SDK starts JSON on a fresh line.
if let Ok(request)=serde_json::from_str::<Value>(&line) && request["jsonrpc"]=="2.0" && request["method"].is_string() {
let host = host.clone();
handlers.spawn(async move {
let method=request["method"].as_str().unwrap();
let params=request.get("params").cloned().unwrap_or(json!({}));
let result=match method {
"core.lock"|"core.unlock"|"core.log"=>host.call(method,params).await,
"core.notify"=>Ok(Value::Null),
_=>Err(json!({"code":-32601,"message":"a test has no other plugins"})),
};
request.get("id").map(|id|match result {Ok(result)=>json!({"jsonrpc":"2.0","id":id,"result":result}),Err(error)=>json!({"jsonrpc":"2.0","id":id,"error":error})})
});
} else {output.push_str(&line);output.push('\n');}
},None=>stdout_open=false,
},
line=stderr.recv(), if stderr_open=>match line {Some(line)=>{output.push_str(&line);output.push('\n');},None=>stderr_open=false},
response=handlers.join_next(), if !handlers.is_empty()=> {
if let Some(response) = response.transpose()?.flatten() {
stdin.write_all(format!("{response}\n").as_bytes()).await?;
stdin.flush().await?;
}
},
}
}
handlers.abort_all();
tracker.idle().await;
host.stop().await;
Ok(Run {
exit: exit.unwrap(),
output,
})
}
/// Extracts libtest names from its stable list format.
fn names(output: &str) -> Vec<String> {
output
.lines()
.filter_map(|line| line.strip_suffix(": test").map(str::to_owned))
.collect()
}
/// Lists tests, selects libtest filters, and runs each selected test on the core pool.
pub async fn run(wasm: &Path, args: Vec<String>) -> Result<bool> {
let package = std::env::current_dir()?;
let cache = std::env::var_os("XDG_CACHE_HOME")
.map(PathBuf::from)
.unwrap_or_else(|| {
PathBuf::from(std::env::var_os("HOME").unwrap_or_default()).join(".cache")
})
.join("sausage");
let compile_path = wasm.to_path_buf();
let (runtime, component) = tokio::task::spawn_blocking(move || -> Result<_> {
let runtime = Runtime::new(&cache)?;
let component = runtime.compile(&std::fs::read(compile_path)?)?;
Ok((runtime, component))
})
.await??;
let mut listing_args = vec![wasm.display().to_string(), "--list".into()];
listing_args.extend(args.iter().filter(|arg| arg.as_str() != "--list").cloned());
let listing = instance(
runtime.clone(),
component.clone(),
listing_args.clone(),
package.clone(),
)
.await?;
if !matches!(listing.exit, Exit::Clean) {
bail!(
"cannot list WASI tests: {}\n{}",
listing.exit,
listing.output
);
}
if args.iter().any(|arg| arg == "--list") {
print!("{}", listing.output);
return Ok(true);
}
let selected = names(&listing.output);
listing_args.push("--ignored".into());
let ignored = if args
.iter()
.any(|arg| matches!(arg.as_str(), "--ignored" | "--include-ignored"))
{
Vec::new()
} else {
names(
&instance(
runtime.clone(),
component.clone(),
listing_args,
package.clone(),
)
.await?
.output,
)
};
println!("running {} tests", selected.len());
let cores = std::thread::available_parallelism()
.map(usize::from)
.unwrap_or(1);
let semaphore = Arc::new(tokio::sync::Semaphore::new(cores));
let mut tasks = tokio::task::JoinSet::new();
let mut skipped = 0;
for name in selected {
if ignored.contains(&name) {
println!("test {name} ... ignored");
skipped += 1;
continue;
}
let runtime = runtime.clone();
let component = component.clone();
let package = package.clone();
let semaphore = semaphore.clone();
let arguments = vec![
wasm.display().to_string(),
"--exact".into(),
name.clone(),
"--nocapture".into(),
"--test-threads=1".into(),
"--include-ignored".into(),
];
tasks.spawn(async move {
let _permit = semaphore.acquire_owned().await?;
let result = instance(runtime, component, arguments, package).await;
Ok::<_, anyhow::Error>((name, result))
});
}
let mut passed = 0;
let mut failed = 0;
while let Some(result) = tasks.join_next().await {
let (name, result) = result??;
let success = result
.as_ref()
.is_ok_and(|run| matches!(run.exit, Exit::Clean));
if success {
passed += 1;
} else {
failed += 1;
}
println!("test {name} ... {}", if success { "ok" } else { "FAILED" });
match result {
Ok(run) => {
for line in run
.output
.lines()
.filter(|line| !line.starts_with("test result:"))
{
// Indentation keeps ordinary test output separate from our libtest status and totals.
println!(" {line}");
}
}
Err(error) => println!("{error:#}"),
}
}
println!(
"\ntest result: {}. {passed} passed; {failed} failed; {skipped} ignored; 0 measured; 0 filtered out",
if failed == 0 { "ok" } else { "FAILED" }
);
Ok(failed == 0)
}