console: one sequential loop; host tests in one binary

The console reads a line, runs it to the end, and prints the result,
with and without a terminal. Notifications print while a command runs.
Ctrl-C during a command stops only the wait. 314 lines (was 480).

All host integration tests are modules of cli/tests/host/, so they run
in one parallel pool, and the probes build once. Warm, the 184 host
tests take 24 s (was about 45 s as seven binaries in sequence), and
cargo test --workspace takes 45 s.
This commit is contained in:
2026-10-23 09:14:26 +01:00
parent e3935031ac
commit 1617dfc1ee
12 changed files with 377 additions and 559 deletions
+237 -403
View File
@@ -1,28 +1,22 @@
//! Console commands, readable results, and line editing with live notifications.
//! Sequential console commands, line editing, and live notifications.
use crate::Host;
use anyhow::Result;
use rustyline::{DefaultEditor, ExternalPrinter, error::ReadlineError};
use sausage_vfs::{Layer, Open};
use serde_json::{Value, json};
use std::time::{SystemTime, UNIX_EPOCH};
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";
ls [path], cat <path>, methods <plugin>
ps, log [plugin or pid] [-n N]
activate <plugin> <wasm>, revert <plugin>
stop <pid>, kill <pid>, help, quit";
async fn read(host: &Host, path: &str) -> Result<Vec<u8>> {
let fs = host.filesystem().await?;
let file = fs
/// Read a complete file through the host filesystem.
async fn read(host: &Host, path: &str) -> Result<String> {
let file = host
.filesystem()
.await?
.open(
path,
Open {
@@ -35,27 +29,72 @@ async fn read(host: &Host, path: &str) -> Result<Vec<u8>> {
loop {
let chunk = file.read(bytes.len() as u64, 65536).await?;
if chunk.is_empty() {
return Ok(bytes);
return Ok(String::from_utf8(bytes)?);
}
bytes.extend(chunk);
}
}
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)?)),
}
/// Recognize the process IDs accepted by stop, kill, and log.
fn is_pid(text: &str) -> bool {
text.rsplit_once('-').is_some()
&& text
.get(text.len().saturating_sub(36)..)
.is_some_and(|id| uuid::Uuid::try_parse(id).is_ok())
}
/// Execute one input line without terminal IO. The caller handles quit after printing.
/// Render method signatures and their documentation.
fn methods(plugin: &str, doc: &Value) -> String {
let mut lines = Vec::new();
for method in doc["methods"].as_array().into_iter().flatten() {
let params = method["params"].as_array().cloned().unwrap_or_default();
let signature = params
.iter()
.map(|p| {
format!(
"{}: {}",
p["name"].as_str().unwrap_or("?"),
p["schema"]["type"].as_str().unwrap_or("any")
)
})
.collect::<Vec<_>>()
.join(", ");
lines.push(format!(
"{plugin}.{}({signature}) -> {}",
method["name"].as_str().unwrap_or("?"),
method["result"]["schema"]["type"].as_str().unwrap_or("any")
));
for field in ["summary", "description"] {
if let Some(text) = method[field].as_str() {
lines.push(format!(" {text}"));
}
}
for p in params {
if let Some(text) = p["description"].as_str() {
lines.push(format!(" {}: {text}", p["name"].as_str().unwrap_or("?")));
}
}
}
lines.join("\n")
}
/// Parse journal filters, or treat invalid arguments as chat.
fn log_params(words: &[&str]) -> Option<Value> {
let mut params = json!({});
let mut words = words.iter();
while let Some(word) = words.next() {
if *word == "-n" {
params["limit"] = json!(words.next()?.parse::<u64>().ok()?);
} else if params.get("plugin").is_none() && params.get("pid").is_none() {
params[if is_pid(word) { "pid" } else { "plugin" }] = json!(word);
} else {
return None;
}
}
Some(params)
}
/// Execute one line and return its unlabelled result.
pub async fn execute_line(host: &Host, line: &str) -> Result<String> {
let line = line.trim();
if line.is_empty() {
@@ -64,84 +103,45 @@ pub async fn execute_line(host: &Host, line: &str) -> Result<String> {
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?;
match (command, words.as_slice()) {
("help", []) => return Ok(HELP.into()),
("quit", []) => return Ok("Goodbye.".into()),
("cat", [path]) => return read(host, path).await,
("ls", [] | [_]) => {
let mut entries = host
.filesystem()
.await?
.list(words.first().copied().unwrap_or("/"))
.await?;
entries.sort_by(|a, b| a.name.cmp(&b.name));
Ok(entries
.into_iter()
return Ok(entries
.iter()
.map(|e| format!("{}{}", e.name, if e.directory { "/" } else { "" }))
.collect::<Vec<_>>()
.join("\n"))
.join("\n"));
}
"cat" if words.len() == 1 => Ok(String::from_utf8(read(host, rest).await?)?),
"methods" if words.len() == 1 => {
("methods", [plugin]) => {
let _activation = host.0.activations.lock().await;
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"))
.map_err(|e| anyhow::anyhow!("{e}"))?;
return Ok(methods(
plugin,
&serde_json::from_str(
&read(host, &format!("/proc/core/plugins/{plugin}/discover.json")).await?,
)?,
));
}
"ps" if words.is_empty() => {
let now = SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis();
("ps", []) => {
let now = std::time::SystemTime::now()
.duration_since(std::time::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)| {
lines.extend(processes.iter().map(|(pid, p)| {
format!(
"{} {} {} {} {} {}s",
pid,
"{pid} {} {} {} {} {}s",
p.plugin,
p.method,
p.parent.as_deref().unwrap_or("-"),
@@ -149,332 +149,166 @@ pub async fn execute_line(host: &Host, line: &str) -> Result<String> {
now.saturating_sub(p.start_time) / 1000
)
}));
Ok(lines.join("\n"))
return Ok(lines.join("\n"));
}
"log" => {
let mut params = json!({});
let mut i = 0;
while i < words.len() {
if words[i] == "-n" {
i += 1;
let Some(count) = words.get(i).and_then(|value| value.parse::<u64>().ok())
else {
return call(host, "agent.chat", json!({"text":line}), true).await;
};
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 {
return call(host, "agent.chat", json!({"text":line}), true).await;
}
i += 1;
}
match host.call("core.log", params).await {
Ok(entries) => entries
.as_array()
.unwrap()
.iter()
.map(|entry| {
let time =
chrono::DateTime::parse_from_rfc3339(entry["time"].as_str().unwrap())?
.with_timezone(&chrono::Local);
let pid = entry["pid"].as_str().unwrap();
let prefix = format!("{}-", entry["plugin"].as_str().unwrap());
let short = pid
.strip_prefix(&prefix)
.and_then(|uuid| uuid.get(..8))
.map(|uuid| format!("{prefix}{uuid}"))
.unwrap_or_else(|| pid.to_owned());
Ok(format!(
"{} {} {} {}",
time.format("%H:%M:%S%.3f"),
short,
entry["stream"].as_str().unwrap(),
entry["line"].as_str().unwrap()
))
})
.collect::<Result<Vec<_>>>()
.map(|lines| lines.join("\n")),
Err(error) => Ok(format!("error: {}", pretty(&error)?)),
}
}
"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
&& words[0].rsplit_once('-').is_some()
&& words[0]
.get(words[0].len().saturating_sub(36)..)
.is_some_and(|id| uuid::Uuid::try_parse(id).is_ok()) =>
{
call(
host,
&format!("core.{command}"),
json!({"pid":words[0]}),
false,
)
.await
}
_ if (rest.is_empty() || rest.starts_with('{'))
&& 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"]
let explicit = (rest.is_empty() || rest.starts_with('{'))
&& command
.split_once('.')
.is_some_and(|(p, m)| !p.is_empty() && !m.is_empty());
let (method, params) = match (command, words.as_slice()) {
("activate", [plugin, wasm]) => {
("core.activate".into(), json!({"plugin":plugin,"wasm":wasm}))
}
("revert", [plugin]) => ("core.revert".into(), json!({"plugin":plugin})),
("stop" | "kill", [pid]) if is_pid(pid) => (format!("core.{command}"), json!({"pid":pid})),
("log", _) if log_params(&words).is_some() => {
("core.log".into(), log_params(&words).unwrap())
}
_ if explicit => (
command.into(),
if rest.is_empty() {
json!({})
} else {
serde_json::from_str(rest)?
},
),
_ => ("agent.chat".into(), json!({"text":line})),
};
let value = match host.call(&method, params).await {
Ok(value) => value,
Err(error) => return Ok(format!("error: {}", serde_json::to_string_pretty(&error)?)),
};
if method == "agent.chat" && !explicit {
return Ok(value["text"]
.as_str()
.or_else(|| payload.as_str())
.or_else(|| value.as_str())
.map(str::to_owned)
.unwrap_or_else(|| payload.to_string());
format!("\x1b[2m{text}\x1b[0m")
} else {
notification.to_string()
.unwrap_or(serde_json::to_string_pretty(&value)?));
}
}
fn indent(text: &str) -> String {
text.lines()
.map(|line| format!(" {line}"))
.collect::<Vec<_>>()
.join("\n")
if method == "core.log" && command == "log" {
return value
.as_array()
.unwrap()
.iter()
.map(|e| {
let time = chrono::DateTime::parse_from_rfc3339(e["time"].as_str().unwrap())?
.with_timezone(&chrono::Local);
let pid = e["pid"].as_str().unwrap();
let prefix = format!("{}-", e["plugin"].as_str().unwrap());
let short = pid
.strip_prefix(&prefix)
.map(|id| format!("{prefix}{}", &id[..8]))
.unwrap_or(pid.into());
Ok(format!(
"{} {short} {} {}",
time.format("%H:%M:%S%.3f"),
e["stream"].as_str().unwrap(),
e["line"].as_str().unwrap()
))
})
.collect::<Result<Vec<_>>>()
.map(|lines| lines.join("\n"));
}
Ok(serde_json::to_string_pretty(&value)?)
}
/// Run the line editor on a dedicated thread, with notifications through its external printer.
/// Without a terminal (for example, input from a pipe), read plain lines instead.
/// Dim text notifications and preserve other payloads.
pub fn notification_text(value: &Value) -> String {
let params = &value["params"];
if params["name"] != "text" {
return value.to_string();
}
let payload = &params["params"];
let text = payload["text"]
.as_str()
.or_else(|| payload.as_str())
.map(str::to_owned)
.unwrap_or(payload.to_string());
format!("\x1b[2m{text}\x1b[0m")
}
/// Read and execute one line at a time. Interrupting a wait does not cancel its host process.
pub async fn run_input(host: Host) -> Result<()> {
if !std::io::IsTerminal::is_terminal(&std::io::stdin()) {
return run_plain(host).await;
}
let terminal = std::io::IsTerminal::is_terminal(&std::io::stdin());
let history = host.0.world.state_dir("core").join("history");
let (ready, started) = tokio::sync::oneshot::channel();
let (sender, lines) = tokio::sync::mpsc::unbounded_channel::<Result<String>>();
// A detached terminal thread does not hold Tokio shutdown open at a blocked readline.
let mut editor = if terminal {
Some(DefaultEditor::new()?)
} else {
None
};
if let Some(editor) = &mut editor {
let _ = editor.load_history(&history);
}
let mut printer = editor
.as_mut()
.map(|e| e.create_external_printer())
.transpose()?;
let (request, requests) =
std::sync::mpsc::channel::<tokio::sync::oneshot::Sender<Result<Option<String>>>>();
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)?;
if sender.send(Ok(line)).is_err() {
break;
for response in requests {
let line = (|| -> Result<_> {
if let Some(editor) = &mut editor {
match editor.readline("> ") {
Ok(line) => {
editor.add_history_entry(&line)?;
editor.save_history(&history)?;
Ok(Some(line))
}
Err(ReadlineError::Interrupted | ReadlineError::Eof) => Ok(None),
Err(e) => Err(e.into()),
}
Err(ReadlineError::Interrupted | ReadlineError::Eof) => {
if sender.send(Ok("quit".into())).is_err() {
break;
}
}
Err(error) => return Err(error.into()),
} else {
std::io::stdin()
.lines()
.next()
.transpose()
.map_err(Into::into)
}
}
Ok(())
})();
if let Err(error) = result {
let _ = sender.send(Err(error));
}
});
let mut printer = started.await??;
run_commands(host, lines, true, move |text| {
printer.print(format!("{text}\n"))?;
Ok(())
})
.await
}
/// Read plain lines from stdin, without line editing or history. A detached
/// thread reads them, so that a blocked read does not hold the runtime open.
async fn run_plain(host: Host) -> Result<()> {
let (sender, lines) = tokio::sync::mpsc::unbounded_channel::<Result<String>>();
std::thread::spawn(move || {
for line in std::io::stdin().lines() {
let line = line.map_err(Into::into);
if sender.send(line).is_err() {
})();
if response.send(line).is_err() {
break;
}
}
});
run_commands(host, lines, false, |text| {
use std::io::Write;
writeln!(std::io::stdout(), "{text}")?;
Ok(())
})
.await
}
// Both input modes label results. Only terminal input starts concurrent commands.
async fn run_commands(
host: Host,
mut lines: tokio::sync::mpsc::UnboundedReceiver<Result<String>>,
concurrent: bool,
mut print: impl FnMut(String) -> Result<()>,
) -> Result<()> {
let mut notifications = host.subscribe();
let mut commands = tokio::task::JoinSet::new();
let mut input_open = true;
let mut notification_task = tokio::task::JoinSet::new();
notification_task.spawn(async move {
loop {
match notifications.recv().await {
Ok(value) => {
let text = notification_text(&value);
if let Some(printer) = &mut printer {
let _ = printer.print(format!("{text}\n"));
} else {
println!("{text}");
}
}
Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
Err(_) => break,
}
}
});
let mut interrupt = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt())?;
loop {
if !input_open && commands.is_empty() {
let (send, line) = tokio::sync::oneshot::channel();
request.send(send)?;
let line =
tokio::select! { result = line => result??, _ = interrupt.recv() => return Ok(()) };
let Some(line) = line else {
return Ok(());
};
let text = tokio::select! {
result = execute_line(&host, &line) => result.unwrap_or_else(|e| format!("error: {e}")),
_ = interrupt.recv() => "stopped waiting; the call continues, see ps".into(),
};
if !text.is_empty() {
println!("{text}");
}
if line.trim() == "quit" {
return Ok(());
}
tokio::select! {
biased;
notification = notifications.recv() => match notification {
Ok(value) => print(indent(&notification_text(&value)))?,
Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
Err(tokio::sync::broadcast::error::RecvError::Closed) => return Ok(()),
},
result = commands.join_next(), if !commands.is_empty() => {
let (line,result): (String,Result<String>) = result.unwrap()?;
let text = result.unwrap_or_else(|error|format!("error: {error}"));
if !text.is_empty() { print(format!("> {line}\n{}",indent(&text)))?; }
},
line = lines.recv(), if input_open && (concurrent || commands.is_empty()) => match line {
Some(line) => {
let line = line?;
if line.trim()=="quit" { print("> quit\n Goodbye.".into())?; return Ok(()); }
let host = host.clone();
commands.spawn(async move {
let result=execute_line(&host,&line).await;
(line,result)
});
},
None => input_open=false,
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn methods_waits_for_activation() {
let folder = tempfile::tempdir().unwrap();
let world = crate::world::World::create(&folder.path().join("world")).unwrap();
let host = Host::start_with_cache(&world.root, &folder.path().join("cache"))
.await
.unwrap();
let guard = host.0.activations.lock().await;
let mut methods = Box::pin(execute_line(&host, "methods missing"));
assert!(
tokio::time::timeout(std::time::Duration::from_millis(100), &mut methods)
.await
.is_err()
);
drop(guard);
assert!(methods.await.is_err());
host.stop().await;
}
#[tokio::test]
async fn commands_remain_available_and_notifications_precede_results() {
let folder = tempfile::tempdir().unwrap();
let world = crate::world::World::create(&folder.path().join("world")).unwrap();
let host = Host::start_with_cache(&world.root, &folder.path().join("cache"))
.await
.unwrap();
host.register_native(
"slow",
std::sync::Arc::new(|ctx, _, _| {
Box::pin(async move {
ctx.notify("ready", json!({"pid":ctx.pid}));
std::future::pending().await
})
}),
);
host.register_native(
"fast",
std::sync::Arc::new(|ctx, _, _| {
Box::pin(async move {
ctx.notify("text", json!("before"));
Ok(json!("after"))
})
}),
);
let mut events = host.subscribe();
let (sender, lines) = tokio::sync::mpsc::unbounded_channel();
let (printed, mut output) = tokio::sync::mpsc::unbounded_channel();
let runner = tokio::spawn(run_commands(host.clone(), lines, true, move |text| {
printed.send(text).unwrap();
Ok(())
}));
sender.send(Ok("slow.wait".into())).unwrap();
let event = events.recv().await.unwrap();
let pid = event["params"]["params"]["pid"].as_str().unwrap();
sender.send(Ok("ps".into())).unwrap();
loop {
let text = output.recv().await.unwrap();
if text.starts_with("> ps") {
assert!(text.contains(pid));
break;
}
}
sender.send(Ok(format!("kill {pid}"))).unwrap();
let mut killed = false;
let mut answered = false;
while !killed || !answered {
let text = output.recv().await.unwrap();
killed |= text.starts_with("> kill ");
answered |= text.starts_with("> slow.wait");
}
sender.send(Ok("fast.go".into())).unwrap();
assert!(output.recv().await.unwrap().contains("before"));
assert!(output.recv().await.unwrap().contains("after"));
sender.send(Ok("quit".into())).unwrap();
runner.await.unwrap().unwrap();
host.stop().await;
}
}
+1 -1
View File
@@ -186,7 +186,7 @@ async fn main() -> Result<std::process::ExitCode> {
let exit = tokio::select! {
_=host.exit_requested()=>true,
_=term.recv()=>false,
_=interrupt.recv()=>false,
_=interrupt.recv(), if cli.headless=>false,
result = console, if !cli.headless => {
console_result=result;
false