Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.
Rust ai-nativedescription-logicsformal-verificationinfrastructure-as-codeisabelle-languageknowledge-graphleanlean4mcpmcp-serverontologyowlowl2reasoningrustshaclsparqltheorem-proving
CLI: daemon mode, marketplace proxy, and --human output (#120)
Fabio Rovaifccfeb4dbd92
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex bf71bc3a..33347229 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -5,6 +5,67 @@ All notable changes to Open Ontologies are documented here.## [Unreleased]### Fixed+- **A daemon started with `[http] token` in config rejected every command it was+ meant to serve.** `serve-http` falls back to the config token and then enforces+ bearer auth, but `daemon start` recorded `token: null` in `daemon.json`+ whenever no `--token` flag or env var was given. Every proxied command then+ sent no `Authorization` header to a daemon demanding one, got 401, and the+ client treated that as fatal, so a single `daemon start` turned the whole CLI+ into an error until `daemon stop`. The token is now resolved the way the child+ will resolve it, from a config path both sides are given explicitly, and a+ daemon that still rejects the client is announced on stderr and fallen back+ from rather than being fatal.+- **`--data-dir` was ignored by the server arms, so a daemon could serve a+ different store than the one its caller was using.** Both `serve` and+ `serve-http` derived their data directory from `[general] data_dir` in config+ and dropped the flag, so `--data-dir /custom daemon start` wrote `daemon.json`+ into `/custom` while the daemon it started served `~/.open-ontologies`. Proxied+ and local commands then saw different data with nothing to indicate it, and+ only when the two paths differed, which is why it survived casual use. The flag+ now takes precedence over config, matching how host, port and token already+ resolved.+- **`push` could not run at all while a daemon was up.** It was serialized for+ proxying but had no arm in `BatchRunner::execute`, so it fell through to+ `unknown batch command` and exited 1 — the only proxy-able command with no+ handler. Running it locally instead would have been worse than the error, since+ the store holding the triples worth pushing is the daemon's. A test now asserts+ that every command the CLI proxies is one the batch runner answers to, because+ the two sides are matched by string across an HTTP boundary and nothing else+ checked that they agreed.+- **Relative paths resolved against the daemon's working directory rather than+ the caller's.** The daemon inherits wherever `daemon start` ran and never+ learns where its caller is, so `cd /data && open-ontologies load ./x.ttl`+ either failed or loaded a different file, and `save ./out.ttl` wrote somewhere+ the caller was not looking. Path arguments are made absolute before they are+ sent.+- **A multi-line or awkwardly quoted argument was destroyed in transit to the+ daemon.** Commands were proxied as a command line that the daemon+ re-tokenized, and `parse_lines` splits on newlines before it looks at quotes,+ so a multi-line SPARQL query — the normal kind — arrived torn across lines and+ failed as an unterminated quote while the identical command succeeded locally.+ The double-quote fallback in the quoting helper also failed to escape+ backslashes, mangling any argument carrying both quote styles. Commands are now+ proxied in the structured form, one argument per array element, where neither+ is possible.+- **A successful `daemon start` reported the daemon it had just started as+ dead.** The human renderer guessed which command produced a payload by+ sniffing its keys, and `{ok, pid, url}` matched a branch written for+ `daemon status` — a branch that defaults liveness to false. That branch now+ requires an explicit `alive`, and on the proxy path the renderer dispatches on+ the command name the batch envelope already carries instead of guessing.+- **`marketplace` answered with a different catalogue depending on whether a+ daemon was running.** The command has two implementations behind it, the local+ one and the batch one that serves it when a daemon is up, and they had drifted:+ the batch copy consulted only the curated catalogue and never loaded community+ packs, so `marketplace list` lost the community tier and `marketplace install`+ refused a community id, with nothing to indicate why. Both now go through+ `marketplace::cli_list` and `marketplace::cli_resolve`. The MCP tool keeps its+ own richer shape, which reports urls, maintainers and shadowing warnings, and+ is documented as a separate surface rather than a third copy of this one.+- **`/api/batch` opened the state database on every request.** The route built a+ fresh `StateDb` per call, on the exact hot path the daemon exists to+ accelerate, while the adjacent `/lineage` route already cloned the handle+ opened at startup. It now clones the same one.- **`temporal:recordedUntil` closes the transaction interval, so `as_of`answers what was believed then.** The recorded axis only ever narrowedforward: with no upper bound, `as_of = now` returned the union of every@@ -154,6 +215,11 @@ All notable changes to Open Ontologies are documented here.the validity scan is cut.### Added+- **CLI: Daemon mode — persistent in-memory store across processes.** New `daemon start / stop / status` subcommand launches `serve-http` as a detached background process and writes its PID + URL to `~/.open-ontologies/daemon.json`. All 24 CLI commands that touch the `Arc<GraphStore>` (load, save, clear, stats, query, lint, reason, shacl, enforce, plan, apply, version, history, rollback, pull, push, ingest, drift, lock, monitor, monitor-clear, marketplace, and `batch`) automatically detect a live daemon and route their request to it via the new `/api/batch` HTTP endpoint — no flags, no code changes per-command. Use `--no-connect` to force local execution. Daemon liveness is checked with a bare `kill(pid, 0)` (Unix) / `tasklist` (Windows), rather than forking `/bin/kill` on the path the daemon exists to make fast; stale `daemon.json` files are removed automatically on the next command. New modules `src/daemon.rs` (PID file management + process control) and `src/connect.rs` (HTTP proxy client), and `libc` as a dependency for the liveness check.++ Commands are proxied in the structured form of `/api/batch` — `{"command": name, "args": [..]}`, one argument per array element — rather than as a command line the daemon re-tokenizes. The lossy round trip was the source of most of what is fixed below: an argument cannot be torn on a newline, mangled by a quoting rule, or resolved against the wrong directory once it is an array element that reaches the daemon exactly as clap parsed it. Path arguments are made absolute before they are sent, and an invocation carrying a flag the batch handler has no arm for is run locally rather than proxied without it. `daemon start` waits for the port to accept a connection instead of sleeping a fixed 600ms, so it neither burns the wait when the bind is fast nor reports success for a child that never bound.+- **CLI: `marketplace` command works via daemon.** `marketplace list [--domain <d>]` and `marketplace install --id <id>` are now proxy-able through the daemon's `/api/batch` endpoint. Installing an ontology from the marketplace with a daemon running loads it into the daemon's persistent store so subsequent `stats`, `query`, and `reason` calls in any other process see the installed triples. Implemented via a new `exec_marketplace` async handler in `BatchRunner`.+- **CLI: Human-readable output behind `--human`.** Passing `--human` renders results as text rather than JSON (e.g. `stats` prints a plain key/value block; `load` prints "Loaded N triples from path"; errors print "Error: message"). JSON remains what every command prints when no format is asked for, so existing scripts and any consumer reading the CLI's stdout are unaffected; `--json` is accepted as an explicit statement of that default, and `--pretty` still gives indented JSON. The branch originally made text the default and JSON opt-in, which `tests/cli_load_ephemeral_test.rs` catches: it runs `load` with no flags and parses stdout, so the flip would have broken every unflagged caller silently, a prose response being detectable as wrong only at the consumer. A `OnceLock<bool>` global (`JSON_MODE`) is resolved once at startup and read through `json_mode()`, which defaults to JSON on any path that runs before startup has set it; `output_json`, `output_result` and both daemon-proxy call sites consult it, so local and proxied commands cannot disagree about the format. Daemon proxy output now matches local output exactly: the `seq`/`command` batch envelope is stripped and only the `result` value is rendered. Human-readable rendering lives in the new `src/output.rs` module (`render_human`), which handles stats tables, SPARQL result tables, marketplace lists, load/save/install confirmation lines, lint/enforce issue lists, version history, SPARQL bindings, and a pretty-JSON fallback for unknown shapes.- **Studio: Multilingual label filter in Tree view.** `TreeView` gains a language chip bar in its header, listing every BCP-47 tag present in the loaded ontology's `rdfs:label` values and shown only when more than one exists. Labels are loaded with full language-tag preservation (two-pass: collect all variants into a `labelMapRef`, then `pickLabel` to build nodes). Switching language relabels the existing React tree state in-place (`relabelTree` walk) — no SPARQL re-query. `nodeMapRef` is also updated so breadcrumbs and connection chips reflect the selected locale. Fallback chain: preferred language → `en` → untagged literal → first available → URI local name.- **Studio: Language badges and filter in Property Inspector.** `PropertyInspector` now parses language tags from both engine-native (`"value"@lang`) and standard SPARQL JSON (`xml:lang`) response formats. Each literal row that carries a language tag displays a small monospace badge (e.g. `en`, `cs`) to the right of the value. A language chip bar above the property list (shown only when ≥1 language tag is detected) lets users filter rows to a single locale; URI values are always shown regardless of the filter. `saveEdit` and `deleteProp` include the original language tag in the SPARQL `DELETE`/`INSERT` pattern so editing one locale does not affect sibling translations. The **+ Add** form gains an optional language tag input with quick-pick chips for languages already present on the node.- **Optional eviction of float32 text vectors from memory**diff --git a/Cargo.lock b/Cargo.lockindex eb3b6aa5..6d2c2d22 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2809,6 +2809,7 @@ dependencies = ["dirs","duckdb","instant-distance",+ "libc","oxigraph","parquet","quick-xml",diff --git a/Cargo.toml b/Cargo.tomlindex dcf31749..0d317767 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -11,6 +11,9 @@ name = "open-ontologies"path = "src/main.rs"[dependencies]+# `is_daemon_alive` runs on every proxy-able invocation; a bare kill(pid, 0)+# avoids forking /bin/kill on the daemon's own hot path.+libc = "0.2"rmcp = { version = "1.4", features = ["server", "transport-io", "transport-streamable-http-server"] }axum = "0.8"tower-http = { version = "0.6", features = ["cors"] }diff --git a/src/batch.rs b/src/batch.rsindex 7a1895ed..f606ead4 100644--- a/src/batch.rs+++ b/src/batch.rs@@ -28,14 +28,34 @@ impl BatchRunner {Self { db, graph, pretty }}- /// Parse input (auto-detect line vs JSON format) and run all commands.+ /// Parse input (auto-detect line vs JSON format) and run all commands,+ /// printing each result as it completes./// Returns the process exit code (0 = success, 1 = at least one error).pub async fn run(&self, input: &str, bail: bool) -> i32 {+ self.run_each(input, bail, |line| self.print_json(line)).await+ }++ /// Run all commands and collect results into a Vec instead of printing.+ /// Returns (results, exit_code).+ pub async fn run_collect(&self, input: &str, bail: bool) -> (Vec<Value>, i32) {+ let mut results = Vec::new();+ let code = self+ .run_each(input, bail, |line| results.push(line.clone()))+ .await;+ (results, code)+ }++ /// The one driver behind `run` and `run_collect`. They differ only in what+ /// they do with each result, so that is the only thing they pass in: keeping+ /// two copies of the loop meant keeping the envelope, the bail handling and+ /// the exit code in step by hand. A sink rather than a returned Vec because+ /// `run` prints as it goes, and a batch of a hundred loads should not go+ /// silent until the last one finishes.+ async fn run_each<F: FnMut(&Value)>(&self, input: &str, bail: bool, mut on_result: F) -> i32 {let commands = match parse_input(input) {Ok(cmds) => cmds,Err(e) => {- let err = json!({"seq": 0, "command": "parse", "error": e});- self.print_json(&err);+ on_result(&json!({"seq": 0, "command": "parse", "error": e}));return 1;}};@@ -44,12 +64,11 @@ impl BatchRunner {for (seq, cmd) in commands.iter().enumerate() {let result = self.execute(cmd).await;let has_error = result.get("error").is_some();- let line = json!({+ on_result(&json!({"seq": seq,"command": cmd.name,"result": result,- });- self.print_json(&line);+ }));if has_error {exit_code = 1;@@ -91,17 +110,39 @@ impl BatchRunner {"rollback" => self.exec_rollback(&cmd.args),"status" => self.exec_status(),"pull" => self.exec_pull(&cmd.args).await,+ "push" => self.exec_push(&cmd.args).await,"ingest" => self.exec_ingest(&cmd.args),"drift" => self.exec_drift(&cmd.args),"lock" => self.exec_lock(&cmd.args),"monitor" => self.exec_monitor(),"monitor-clear" => self.exec_monitor_clear(),+ "marketplace" => self.exec_marketplace(&cmd.args).await,_ => json!({"error": format!("unknown batch command: '{}'", cmd.name)}),}}// ─── Command implementations ─────────────────────────────────────+ /// `push` was serialized by the proxy and had no arm here, so it was the one+ /// proxy-able command that could not run: it fell through to+ /// `unknown batch command` and exited 1 whenever a daemon was up. Running it+ /// locally instead would be worse than an error, because the store holding+ /// the triples worth pushing is the daemon's.+ async fn exec_push(&self, args: &[String]) -> Value {+ let endpoint = match args.first() {+ Some(e) => e,+ None => return json!({"error": "push requires an endpoint"}),+ };+ let content = match self.graph.serialize("ntriples") {+ Ok(c) => c,+ Err(e) => return json!({"error": e.to_string()}),+ };+ match GraphStore::push_sparql(endpoint, &content).await {+ Ok(msg) => json!({"ok": true, "message": msg}),+ Err(e) => json!({"error": e.to_string()}),+ }+ }+fn exec_load(&self, args: &[String]) -> Value {let path = match args.first() {Some(p) => p,@@ -463,6 +504,53 @@ impl BatchRunner {json!({"ok": true, "message": "Monitor block cleared"})}+ async fn exec_marketplace(&self, args: &[String]) -> Value {+ use crate::marketplace;+ let action = match args.first() {+ Some(a) => a.as_str(),+ None => return json!({"error": "marketplace requires 'list' or 'install'"}),+ };+ match action {+ "list" => {+ let domain = Self::flag_value(args, "--domain");+ let (items, community_error) = marketplace::cli_list(domain.as_deref()).await;+ json!({+ "count": items.len(),+ "ontologies": items,+ "community_registry_error": community_error,+ })+ }+ "install" => {+ let id = match Self::flag_value(args, "--id") {+ Some(id) => id,+ None => return json!({"error": "marketplace install requires --id"}),+ };+ let pack = match marketplace::cli_resolve(&id).await {+ Ok(p) => p,+ Err(e) => return json!({"error": e}),+ };+ let content = match crate::graph::GraphStore::fetch_url(&pack.url).await {+ Ok(c) => c,+ Err(e) => return json!({"error": e.to_string()}),+ };+ match self.graph.load_content_with_base(&content, pack.format, Some(&pack.url)) {+ Ok(count) => {+ let stats = self.graph.get_stats().unwrap_or_default();+ json!({+ "ok": true,+ "installed": pack.id,+ "name": pack.name,+ "triples_loaded": count,+ "stats": serde_json::from_str::<serde_json::Value>(&stats).unwrap_or_default(),+ })+ }+ Err(e) => json!({"error": format!("Parse error: {}", e)}),+ }+ }+ _ => json!({"error": format!("Unknown marketplace action: '{}'. Use 'list' or 'install'.", action)}),+ }+ }+// ─── Helpers ─────────────────────────────────────────────────────/// Extract --flag value from args (e.g. --format turtle → Some("turtle"))diff --git a/src/connect.rs b/src/connect.rsnew file mode 100644index 00000000..c0d0e183--- /dev/null+++ b/src/connect.rs@@ -0,0 +1,96 @@+//! HTTP client that proxies CLI batch commands to a running daemon.++use crate::daemon::DaemonInfo;++/// POST batch input to the daemon's `/api/batch` endpoint and stream results to+/// stdout. Output matches local CLI format: JSON unless `json` is false.+///+/// `Ok(Some(code))` means the daemon ran the commands and this is the exit code.+/// `Ok(None)` means the daemon could not be used and the caller should run+/// locally; the reason has already been written to stderr. That distinction is+/// the difference between a working CLI and a bricked one: a daemon started+/// while `[http] token` was set in config enforces bearer auth, and a client+/// that treats the resulting 401 as fatal turns every proxy-able command into an+/// error until `daemon stop`. Falling back is announced rather than silent,+/// because the local store is not necessarily the daemon's.+pub async fn proxy_batch(+ info: &DaemonInfo,+ input: &str,+ bail: bool,+ json: bool,+ pretty: bool,+) -> anyhow::Result<Option<i32>> {+ let client = reqwest::Client::new();+ let url = format!("{}/api/batch{}", info.url, if bail { "?bail=true" } else { "" });++ // The endpoint sniffs the body rather than the header, but saying which form+ // is being sent costs nothing and keeps the request honest.+ let content_type = if input.trim_start().starts_with('[') {+ "application/json"+ } else {+ "text/plain"+ };++ let mut req = client+ .post(&url)+ .header("Content-Type", content_type)+ .body(input.to_string());++ if let Some(ref token) = info.token {+ req = req.header("Authorization", format!("Bearer {}", token));+ }++ let response = match req.send().await {+ Ok(r) => r,+ Err(e) => {+ // The liveness check passed a moment ago, so this is a daemon that+ // died in between, or one bound somewhere we cannot reach.+ eprintln!("warning: daemon at {} is unreachable ({e}); running locally", info.url);+ return Ok(None);+ }+ };++ if !response.status().is_success() {+ let status = response.status();+ let body = response.text().await.unwrap_or_default();+ if status == reqwest::StatusCode::UNAUTHORIZED+ || status == reqwest::StatusCode::FORBIDDEN+ {+ eprintln!(+ "warning: daemon at {} rejected authentication ({status}); running locally",+ info.url+ );+ return Ok(None);+ }+ anyhow::bail!("daemon returned {}: {}", status, body);+ }++ let items: Vec<serde_json::Value> = response+ .json()+ .await+ .map_err(|e| anyhow::anyhow!("failed to parse daemon response: {}", e))?;++ let mut exit_code = 0;+ for item in &items {+ // Unwrap the batch envelope — print only the result, matching local+ // output — but keep the command name, which is the discriminator the+ // human renderer needs to dispatch exactly instead of guessing from+ // which keys a payload happens to carry.+ let command = item.get("command").and_then(|c| c.as_str());+ let result = item.get("result").unwrap_or(item);+ if json || pretty {+ if pretty {+ println!("{}", serde_json::to_string_pretty(result).unwrap());+ } else {+ println!("{}", result);+ }+ } else {+ println!("{}", crate::output::render_human_for(command, result));+ }+ if result.get("error").is_some() {+ exit_code = 1;+ }+ }++ Ok(Some(exit_code))+}diff --git a/src/daemon.rs b/src/daemon.rsnew file mode 100644index 00000000..af1bfaad--- /dev/null+++ b/src/daemon.rs@@ -0,0 +1,237 @@+//! Background daemon management — start/stop/status for the persistent HTTP store.++use std::path::PathBuf;+use serde::{Deserialize, Serialize};++#[derive(Debug, Clone, Serialize, Deserialize)]+pub struct DaemonInfo {+ pub pid: u32,+ pub url: String,+ pub token: Option<String>,+}++pub fn daemon_file_path(data_dir: &str) -> PathBuf {+ let data_dir = crate::config::expand_tilde(data_dir);+ PathBuf::from(data_dir).join("daemon.json")+}++pub fn read_daemon_info(data_dir: &str) -> Option<DaemonInfo> {+ let path = daemon_file_path(data_dir);+ let content = std::fs::read_to_string(&path).ok()?;+ serde_json::from_str(&content).ok()+}++fn write_daemon_info(data_dir: &str, info: &DaemonInfo) -> anyhow::Result<()> {+ let path = daemon_file_path(data_dir);+ std::fs::write(path, serde_json::to_string_pretty(info)?)?;+ Ok(())+}++pub fn remove_daemon_info(data_dir: &str) {+ let _ = std::fs::remove_file(daemon_file_path(data_dir));+}++/// Returns true if the process with the given PID is alive.+///+/// On unix this is a bare `kill(pid, 0)` rather than fork+exec of `/bin/kill`:+/// it runs on every proxy-able invocation, which is the exact path the daemon+/// exists to make fast, and spawning a process to ask whether a process exists+/// is most of that path's cost. `EPERM` counts as alive — the process is there,+/// this user simply may not signal it — and a non-positive PID is rejected+/// outright, because `kill(0, ...)` addresses the whole process group.+pub fn is_daemon_alive(pid: u32) -> bool {+ if pid == 0 {+ return false;+ }+ #[cfg(unix)]+ {+ let rc = unsafe { libc::kill(pid as libc::pid_t, 0) };+ if rc == 0 {+ return true;+ }+ std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)+ }+ #[cfg(not(unix))]+ {+ std::process::Command::new("tasklist")+ .args(["/FI", &format!("PID eq {}", pid), "/NH"])+ .output()+ .map(|o| String::from_utf8_lossy(&o.stdout).contains(&pid.to_string()))+ .unwrap_or(false)+ }+}++/// Start `serve-http` in the background and write daemon.json.+/// Returns the `DaemonInfo` on success.+pub fn start_daemon(+ data_dir: &str,+ host: &str,+ port: u16,+ token: Option<String>,+) -> anyhow::Result<DaemonInfo> {+ let exe = std::env::current_exe()+ .map_err(|e| anyhow::anyhow!("cannot find current binary: {}", e))?;++ let data_dir_expanded = crate::config::expand_tilde(data_dir);++ // Name the config file explicitly instead of letting the child fall back to+ // its own default. Both sides then read one file, which is what makes the+ // token below the token the child will actually enforce, and what keeps a+ // non-default --data-dir from starting a daemon configured by a different+ // directory's config.+ let config_path = PathBuf::from(&data_dir_expanded).join("config.toml");++ // `serve-http` falls back to `[http] token` in config when no flag or env+ // var is given, and then enforces bearer auth. Recording `None` here because+ // no flag was passed left every proxied command sending no Authorization+ // header to a daemon demanding one: 401 on everything until `daemon stop`.+ // Resolve it the same way the child will, and record what it resolves to.+ let token = token.or_else(|| {+ crate::config::Config::load(&config_path)+ .ok()+ .and_then(|c| crate::config::resolve_http_token(&c.http))+ });++ let mut cmd = std::process::Command::new(&exe);+ cmd.arg("--data-dir").arg(&data_dir_expanded);+ cmd.arg("serve-http");+ cmd.arg("--config").arg(&config_path);+ cmd.arg("--host").arg(host);+ cmd.arg("--port").arg(port.to_string());+ if let Some(ref t) = token {+ cmd.arg("--token").arg(t);+ }+ cmd.stdout(std::process::Stdio::null());+ cmd.stderr(std::process::Stdio::null());++ let child = cmd.spawn()+ .map_err(|e| anyhow::anyhow!("failed to spawn daemon: {}", e))?;++ let pid = child.id();+ // Detach: forget the Child so it becomes an orphan adopted by init.+ std::mem::forget(child);++ // Wait for the port to accept a connection rather than sleeping a fixed+ // 600ms. The fixed sleep was wrong in both directions: it burned the whole+ // 600ms when the bind took 20, and it reported success when the child had+ // already died or never bound at all, so `daemon start` printed a PID and+ // URL for something that was not listening and every later command then+ // failed against it.+ let probe_host = if host == "0.0.0.0" || host == "::" { "127.0.0.1" } else { host };+ let addr = format!("{}:{}", probe_host, port);+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);+ let mut bound = false;+ while std::time::Instant::now() < deadline {+ if std::net::TcpStream::connect(&addr).is_ok() {+ bound = true;+ break;+ }+ if !is_daemon_alive(pid) {+ anyhow::bail!("daemon exited before binding {} (pid {})", addr, pid);+ }+ std::thread::sleep(std::time::Duration::from_millis(25));+ }+ if !bound {+ anyhow::bail!("daemon did not bind {} within 10s (pid {})", addr, pid);+ }++ let url = format!("http://{}:{}", host, port);+ let info = DaemonInfo { pid, url, token };+ write_daemon_info(data_dir, &info)?;+ Ok(info)+}++/// Kill the daemon and remove daemon.json.+pub fn stop_daemon(data_dir: &str) -> anyhow::Result<()> {+ let info = read_daemon_info(data_dir)+ .ok_or_else(|| anyhow::anyhow!("no daemon.json found — is the daemon running?"))?;++ if !is_daemon_alive(info.pid) {+ remove_daemon_info(data_dir);+ anyhow::bail!("daemon PID {} is not running (stale daemon.json removed)", info.pid);+ }++ #[cfg(unix)]+ {+ let status = std::process::Command::new("kill")+ .args([&info.pid.to_string()])+ .status()+ .map_err(|e| anyhow::anyhow!("kill failed: {}", e))?;+ if !status.success() {+ anyhow::bail!("kill {} failed with status {}", info.pid, status);+ }+ }+ #[cfg(not(unix))]+ {+ std::process::Command::new("taskkill")+ .args(["/PID", &info.pid.to_string(), "/F"])+ .status()+ .map_err(|e| anyhow::anyhow!("taskkill failed: {}", e))?;+ }++ remove_daemon_info(data_dir);+ Ok(())+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn the_current_process_is_alive_and_pid_zero_is_not() {+ assert!(is_daemon_alive(std::process::id()));+ // `kill(0, sig)` addresses the caller's whole process group, so a zero+ // PID must be rejected before it reaches the syscall rather than+ // reported as a living daemon.+ assert!(!is_daemon_alive(0));+ }++ #[test]+ fn a_pid_that_cannot_exist_is_not_alive() {+ // Above every platform's pid_max, so this is never a live process.+ assert!(!is_daemon_alive(u32::MAX - 1));+ }++ #[test]+ fn daemon_info_round_trips_through_the_data_dir() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().to_string_lossy().to_string();+ assert!(read_daemon_info(&path).is_none());++ let info = DaemonInfo {+ pid: 4242,+ url: "http://127.0.0.1:8080".into(),+ token: Some("s3cret".into()),+ };+ write_daemon_info(&path, &info).unwrap();++ let read = read_daemon_info(&path).expect("daemon.json is readable");+ assert_eq!(read.pid, 4242);+ assert_eq!(read.url, "http://127.0.0.1:8080");+ // The token has to survive the round trip: it is what the proxy sends as+ // its bearer credential, and dropping it is a 401 on every command.+ assert_eq!(read.token.as_deref(), Some("s3cret"));++ remove_daemon_info(&path);+ assert!(read_daemon_info(&path).is_none());+ }++ #[test]+ fn a_stale_daemon_file_is_detectable_as_stale() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().to_string_lossy().to_string();+ write_daemon_info(+ &path,+ &DaemonInfo { pid: u32::MAX - 1, url: "http://127.0.0.1:8080".into(), token: None },+ )+ .unwrap();++ let info = read_daemon_info(&path).unwrap();+ assert!(!is_daemon_alive(info.pid), "a dead PID must read as dead");++ // Which is what lets the caller clear it and fall through to local+ // execution instead of proxying into nothing.+ remove_daemon_info(&path);+ assert!(read_daemon_info(&path).is_none());+ }+}diff --git a/src/lib.rs b/src/lib.rsindex 3b947995..4c44cd01 100644--- a/src/lib.rs+++ b/src/lib.rs@@ -1,6 +1,9 @@pub mod align;pub mod align_fuzzy;pub mod batch;+pub mod connect;+pub mod daemon;+pub mod output;pub mod borderline_loop;pub mod buffer;pub mod cache;diff --git a/src/main.rs b/src/main.rsindex bc37a80b..8a2ff229 100644--- a/src/main.rs+++ b/src/main.rs@@ -152,6 +152,10 @@ data_dir = "~/.open-ontologies"# # file = "/var/log/open-ontologies.log""#;+/// The default for `--data-dir`, named so the server arms can tell an explicit+/// `--data-dir` from an unset one and give the flag precedence over config.+const DEFAULT_DATA_DIR: &str = "~/.open-ontologies";+#[derive(Parser)]#[command(name = "open-ontologies",@@ -161,13 +165,27 @@ struct Cli {#[command(subcommand)]command: Commands,- /// Pretty-print JSON output+ /// Output results as JSON. This is the default; the flag is accepted so+ /// that scripts can state the format they depend on rather than assume it.+ #[arg(long, global = true)]+ json: bool,++ /// Render results as human-readable text instead of JSON. Overridden by+ /// --json or --pretty when both are given.+ #[arg(long, global = true)]+ human: bool,++ /// Pretty-print JSON output (implies --json)#[arg(long, global = true)]pretty: bool,/// Data directory (default: ~/.open-ontologies)- #[arg(long, global = true, default_value = "~/.open-ontologies")]+ #[arg(long, global = true, default_value = DEFAULT_DATA_DIR)]data_dir: String,++ /// Force local execution — ignore a running daemon even if one is detected+ #[arg(long, global = true)]+ no_connect: bool,}#[derive(Subcommand)]@@ -302,6 +320,13 @@ enum Commands {bail: bool,},+ // ─── Daemon ───────────────────────────────────────────────────+ /// Manage background daemon (persistent in-memory store via serve-http)+ Daemon {+ #[command(subcommand)]+ action: DaemonAction,+ },+// ─── Core ontology ────────────────────────────────────────────/// Validate RDF/OWL syntax (file or stdin with -)Validate { input: String },@@ -559,6 +584,195 @@ enum Commands {},}+impl Commands {+ /// Serialize a proxy-able command into the structured form of `/api/batch`:+ /// `{"command": name, "args": [..]}`, where every argument is its own array+ /// element and reaches the daemon exactly as clap parsed it.+ ///+ /// The previous shape of this was a shell-ish line that the daemon+ /// re-tokenized, and the round trip lost things. `parse_lines` splits on+ /// newlines before it looks at quotes, so a multi-line SPARQL query — the+ /// normal kind — arrived torn across lines and failed as an unterminated+ /// quote, while the identical command run locally succeeded. The+ /// double-quote fallback in the quoting helper did not escape backslashes,+ /// so an argument holding both quote styles came out mangled. Neither can+ /// happen to an array element.+ ///+ /// Returns None for commands that must always run locally (server modes,+ /// daemon management, static file operations), and for any invocation+ /// carrying a flag the batch handler cannot honour, which is safer than+ /// proxying it and dropping the flag in silence.+ fn to_batch_command(&self) -> Option<serde_json::Value> {+ fn cmd(name: &str, args: Vec<String>) -> Option<serde_json::Value> {+ Some(serde_json::json!({"command": name, "args": args}))+ }+ match self {+ Commands::Load { path } => cmd("load", vec![absolutize(path)]),+ Commands::Save { path, format } => cmd(+ "save",+ vec![absolutize(path), "--format".into(), format.clone()],+ ),+ Commands::Clear => cmd("clear", vec![]),+ Commands::Stats => cmd("stats", vec![]),+ Commands::Query { query } => cmd("query", vec![query.clone()]),+ Commands::Lint { input } => cmd("lint", vec![absolutize(input)]),+ Commands::Reason { profile } => {+ cmd("reason", vec!["--profile".into(), profile.clone()])+ }+ Commands::Shacl { shapes } => cmd("shacl", vec![absolutize(shapes)]),+ Commands::Status => cmd("status", vec![]),+ Commands::Pull { url, sparql, query } => {+ let mut a = vec![url.clone()];+ if *sparql {+ a.push("--sparql".into());+ }+ if let Some(q) = query {+ a.push("--query".into());+ a.push(q.clone());+ }+ cmd("pull", a)+ }+ Commands::Push { endpoint, graph } => {+ let mut a = vec![endpoint.clone()];+ if let Some(g) = graph {+ a.push("--graph".into());+ a.push(g.clone());+ }+ cmd("push", a)+ }+ Commands::Version { label } => cmd("version", vec![label.clone()]),+ Commands::History => cmd("history", vec![]),+ Commands::Rollback { label } => cmd("rollback", vec![label.clone()]),+ // `--format` has no arm in the batch ingester, so an invocation+ // carrying it runs locally rather than being proxied without it.+ Commands::Ingest { path, format, mapping, base_iri } => {+ if format.is_some() {+ return None;+ }+ let mut a = vec![absolutize(path)];+ if let Some(m) = mapping {+ a.push("--mapping".into());+ a.push(absolutize(m));+ }+ if let Some(b) = base_iri {+ a.push("--base-iri".into());+ a.push(b.clone());+ }+ cmd("ingest", a)+ }+ Commands::Plan { file } => cmd("plan", vec![absolutize(file)]),+ Commands::Apply { mode, plan_id } => {+ let mut a = vec![mode.clone()];+ if let Some(p) = plan_id {+ a.push("--plan-id".into());+ a.push(p.clone());+ }+ cmd("apply", a)+ }+ Commands::Enforce { pack } => cmd("enforce", vec![pack.clone()]),+ Commands::Monitor => cmd("monitor", vec![]),+ Commands::MonitorClear => cmd("monitor-clear", vec![]),+ Commands::Drift { file_a, file_b } => {+ cmd("drift", vec![absolutize(file_a), absolutize(file_b)])+ }+ Commands::Lock { iris, reason } => {+ let mut a: Vec<String> = iris.clone();+ if let Some(r) = reason {+ a.push("--reason".into());+ a.push(r.clone());+ }+ cmd("lock", a)+ }+ Commands::Marketplace { action, id, domain } => {+ let mut a = vec![action.clone()];+ if let Some(i) = id {+ a.push("--id".into());+ a.push(i.clone());+ }+ if let Some(d) = domain {+ a.push("--domain".into());+ a.push(d.clone());+ }+ cmd("marketplace", a)+ }+ // Never proxied: server modes, daemon, init, validate, diff, convert,+ // import-owl, map, extend, align, feedback, lineage, clinical, schema ops.+ _ => None,+ }+ }+}++/// Make a path argument absolute against the caller's working directory before+/// it is proxied.+///+/// The daemon inherits whatever directory `daemon start` ran in and never learns+/// the caller's, so a relative path forwarded verbatim resolved against the wrong+/// place: `cd /data && onto load ./x.ttl` either failed or loaded a different+/// file, and `save ./out.ttl` wrote somewhere the caller was not looking. Not+/// `canonicalize`, which requires the path to exist and so cannot be used for an+/// output path that is about to be created.+/// Hand `input` to a running daemon, or report that there is none to hand it to.+///+/// `Some(exit_code)` means the daemon ran it and the caller should exit with+/// that code; `None` means run locally. Both proxy sites went through their own+/// copy of this and the copies had already drifted — one hardcoded `bail` to+/// false — so there is one copy now, and one place where the decision to proxy+/// is made.+async fn proxy_if_daemon(ctx: &ProxyCtx, input: &str, bail: bool) -> anyhow::Result<Option<i32>> {+ if ctx.no_connect {+ return Ok(None);+ }+ let Some(info) = open_ontologies::daemon::read_daemon_info(&ctx.data_dir) else {+ return Ok(None);+ };+ if !open_ontologies::daemon::is_daemon_alive(info.pid) {+ // Stale daemon.json — clean up silently and fall through to local.+ open_ontologies::daemon::remove_daemon_info(&ctx.data_dir);+ return Ok(None);+ }+ open_ontologies::connect::proxy_batch(&info, input, bail, json_mode(), ctx.pretty).await+}++/// The parts of `Cli` the proxy needs, taken before `cli.command` is matched by+/// value and its fields move out.+struct ProxyCtx {+ no_connect: bool,+ data_dir: String,+ pretty: bool,+}++fn absolutize(path: &str) -> String {+ let expanded = expand_tilde(path);+ let p = std::path::Path::new(&expanded);+ if p.is_absolute() {+ return expanded;+ }+ match std::env::current_dir() {+ Ok(cwd) => cwd.join(p).to_string_lossy().into_owned(),+ Err(_) => expanded,+ }+}++#[derive(Subcommand)]+enum DaemonAction {+ /// Start the daemon (serve-http in background, persistent in-memory store)+ Start {+ /// Host to bind to (default: 127.0.0.1)+ #[arg(long, default_value = "127.0.0.1")]+ host: String,+ /// Port to bind to (default: 8080)+ #[arg(long, default_value = "8080")]+ port: u16,+ /// Optional bearer token for authentication+ #[arg(long, env = "OPEN_ONTOLOGIES_TOKEN")]+ token: Option<String>,+ },+ /// Stop the running daemon+ Stop,+ /// Show daemon status (pid, url, alive/dead)+ Status,+}+fn setup(data_dir: &str) -> anyhow::Result<(StateDb, Arc<GraphStore>)> {let data_dir = expand_tilde(data_dir);let data_path = std::path::Path::new(&data_dir);@@ -577,6 +791,16 @@ fn setup(data_dir: &str) -> anyhow::Result<(StateDb, Arc<GraphStore>)> {Ok((db, graph))}+/// False only when --human is passed without --json or --pretty; checked by+/// output_json/output_result.+static JSON_MODE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();++/// The resolved output format. Defaults to JSON on the paths that read it+/// before `async_main` has set it, so no caller can fall into human-readable+/// output by accident.+fn json_mode() -> bool {+ JSON_MODE.get().copied().unwrap_or(true)+}/// Whether the resolved storage backend keeps the graph in RAM only.////// One-shot CLI subcommands each build their own store, so in this mode nothing@@ -616,24 +840,36 @@ fn build_main_graph(}fn output_json(value: &serde_json::Value, pretty: bool) {- if pretty {- println!("{}", serde_json::to_string_pretty(value).unwrap());+ if json_mode() || pretty {+ if pretty {+ println!("{}", serde_json::to_string_pretty(value).unwrap());+ } else {+ println!("{}", value);+ }} else {- println!("{}", value);+ println!("{}", open_ontologies::output::render_human(value));}}/// Print a JSON string result, with optional pretty-printing./// Handles the common pattern of domain functions returning String results.fn output_result(result: &str, pretty: bool) {- if pretty {- if let Ok(v) = serde_json::from_str::<serde_json::Value>(result) {- println!("{}", serde_json::to_string_pretty(&v).unwrap());+ if json_mode() || pretty {+ if pretty {+ if let Ok(v) = serde_json::from_str::<serde_json::Value>(result) {+ println!("{}", serde_json::to_string_pretty(&v).unwrap());+ } else {+ println!("{}", result);+ }} else {println!("{}", result);}} else {- println!("{}", result);+ if let Ok(v) = serde_json::from_str::<serde_json::Value>(result) {+ println!("{}", open_ontologies::output::render_human(&v));+ } else {+ println!("{}", result);+ }}}@@ -817,6 +1053,23 @@ fn main() -> anyhow::Result<()> {async fn async_main() -> anyhow::Result<()> {let cli = Cli::parse();+ // Activate JSON mode when --json or --pretty is passed.+ JSON_MODE.set(cli.json || cli.pretty || !cli.human).ok();++ let proxy_ctx = ProxyCtx {+ no_connect: cli.no_connect,+ data_dir: cli.data_dir.clone(),+ pretty: cli.pretty,+ };++ // If a daemon is running and this command is proxy-able, route to it.+ if let Some(batch) = cli.command.to_batch_command() {+ let payload = serde_json::to_string(&vec![batch])?;+ if let Some(code) = proxy_if_daemon(&proxy_ctx, &payload, false).await? {+ std::process::exit(code);+ }+ }+match cli.command {Commands::Init {data_dir,@@ -945,7 +1198,13 @@ async fn async_main() -> anyhow::Result<()> {// prefix, feedback thresholds, repo / imports / webhook).open_ontologies::runtime::init_from_config(&cfg);- let data_dir = expand_tilde(&cfg.general.data_dir);+ // Same precedence as the HTTP arm: an explicit --data-dir wins over+ // config rather than being ignored.+ let data_dir = if cli.data_dir != DEFAULT_DATA_DIR {+ expand_tilde(&cli.data_dir)+ } else {+ expand_tilde(&cfg.general.data_dir)+ };let data_path = std::path::Path::new(&data_dir);let db_path = data_path.join("open-ontologies.db");@@ -1050,7 +1309,17 @@ async fn async_main() -> anyhow::Result<()> {// neither is set.let token = token.or_else(|| open_ontologies::config::resolve_http_token(&cfg.http));- let data_dir = expand_tilde(&cfg.general.data_dir);+ // `--data-dir` used to be dropped here, so `--data-dir /custom daemon+ // start` wrote daemon.json into /custom while the daemon it started+ // served ~/.open-ontologies: proxied and local commands then saw+ // different stores and nothing said so. Invisible whenever both+ // resolve to the same directory, which is why it survived testing.+ // Precedence matches host/port/token above: CLI over config.+ let data_dir = if cli.data_dir != DEFAULT_DATA_DIR {+ expand_tilde(&cli.data_dir)+ } else {+ expand_tilde(&cfg.general.data_dir)+ };let data_path_owned = std::path::PathBuf::from(&data_dir);let db_path_owned = data_path_owned.join("open-ontologies.db");@@ -1174,6 +1443,8 @@ async fn async_main() -> anyhow::Result<()> {let sg_load = shared_graph.clone();let sg_save = shared_graph.clone();let sg_load_turtle = shared_graph.clone();+ let sg_batch = shared_graph.clone();+ let db_for_batch = shared_db.clone();let api = axum::Router::new().route("/stats", axum::routing::get(move || {let g = sg_stats.clone();@@ -1244,6 +1515,26 @@ async fn async_main() -> anyhow::Result<()> {).unwrap_or_default())}}))+ .route("/batch", axum::routing::post(move |req: axum::extract::Request| {+ let g = sg_batch.clone();+ let db = db_for_batch.clone();+ async move {+ let bail = req.uri().query()+ .map(|q| q.contains("bail=true"))+ .unwrap_or(false);+ let body = axum::body::to_bytes(req.into_body(), usize::MAX).await+ .unwrap_or_default();+ let input = String::from_utf8_lossy(&body).to_string();+ // The handle opened at startup, not a fresh+ // `StateDb::open` per request: this is the hot path the+ // daemon exists to make fast, and it was paying a SQLite+ // open on every call while the adjacent /lineage route+ // already cloned the shared one.+ let runner = open_ontologies::batch::BatchRunner::new(db, g, false);+ let (results, _) = runner.run_collect(&input, bail).await;+ (axum::http::StatusCode::OK, axum::Json(serde_json::json!(results)))+ }+ })).route("/lineage", axum::routing::get(move || {let db = shared_db.clone();async move {@@ -1413,7 +1704,6 @@ async fn async_main() -> anyhow::Result<()> {// ─── Batch ──────────────────────────────────────────────────Commands::Batch { input, bail } => {- let (db, graph) = setup(&cli.data_dir)?;let batch_input = if input == "-" {let mut buf = String::new();std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)?;@@ -1421,11 +1711,66 @@ async fn async_main() -> anyhow::Result<()> {} else {std::fs::read_to_string(&input)?};+ // Route to daemon if running.+ if let Some(code) = proxy_if_daemon(&proxy_ctx, &batch_input, bail).await? {+ std::process::exit(code);+ }+ let (db, graph) = setup(&cli.data_dir)?;let runner = open_ontologies::batch::BatchRunner::new(db, graph, cli.pretty);let exit_code = runner.run(&batch_input, bail).await;std::process::exit(exit_code);}+ // ─── Daemon ─────────────────────────────────────────────────+ Commands::Daemon { action } => {+ match action {+ DaemonAction::Start { host, port, token } => {+ // Check if already running.+ if let Some(info) = open_ontologies::daemon::read_daemon_info(&cli.data_dir)+ && open_ontologies::daemon::is_daemon_alive(info.pid) {+ output_json(&serde_json::json!({+ "error": format!("daemon already running (pid {})", info.pid),+ "url": info.url,+ }), cli.pretty);+ std::process::exit(1);+ }+ match open_ontologies::daemon::start_daemon(&cli.data_dir, &host, port, token) {+ Ok(info) => output_json(&serde_json::json!({+ "ok": true,+ "pid": info.pid,+ "url": info.url,+ }), cli.pretty),+ Err(e) => {+ output_json(&serde_json::json!({"error": e.to_string()}), cli.pretty);+ std::process::exit(1);+ }+ }+ }+ DaemonAction::Stop => {+ match open_ontologies::daemon::stop_daemon(&cli.data_dir) {+ Ok(()) => output_json(&serde_json::json!({"ok": true, "message": "daemon stopped"}), cli.pretty),+ Err(e) => {+ output_json(&serde_json::json!({"error": e.to_string()}), cli.pretty);+ std::process::exit(1);+ }+ }+ }+ DaemonAction::Status => {+ match open_ontologies::daemon::read_daemon_info(&cli.data_dir) {+ None => output_json(&serde_json::json!({"alive": false, "message": "no daemon.json found"}), cli.pretty),+ Some(info) => {+ let alive = open_ontologies::daemon::is_daemon_alive(info.pid);+ output_json(&serde_json::json!({+ "alive": alive,+ "pid": info.pid,+ "url": info.url,+ }), cli.pretty);+ }+ }+ }+ }+ }+// ─── Core ontology ─────────────────────────────────────────Commands::Validate { input } => {let result = if input == "-" {@@ -1589,39 +1934,8 @@ async fn async_main() -> anyhow::Result<()> {use open_ontologies::marketplace;match action.as_str() {"list" => {- let entries = marketplace::list(domain.as_deref());- let mut items: Vec<serde_json::Value> = entries- .iter()- .map(|e| {- serde_json::json!({- "id": e.id,- "name": e.name,- "description": e.description,- "domain": e.domain,- "format": marketplace::format_name(e.format),- "source": "curated",- })- })- .collect();- let mut community_error = None;- match marketplace::load_community_packs().await {- Ok((packs, _shadowed, _source)) => {- for p in packs- .iter()- .filter(|p| domain.as_deref().is_none_or(|d| p.domain == d))- {- items.push(serde_json::json!({- "id": p.id,- "name": p.name,- "description": p.description,- "domain": p.domain,- "format": p.format,- "source": "community",- }));- }- }- Err(e) => community_error = Some(e),- }+ let (items, community_error) =+ marketplace::cli_list(domain.as_deref()).await;output_json(&serde_json::json!({"count": items.len(),@@ -1637,29 +1951,15 @@ async fn async_main() -> anyhow::Result<()> {std::process::exit(1);});// Curated first; community packs can never shadow a curated ID.+ let pack = match marketplace::cli_resolve(id).await {+ Ok(p) => p,+ Err(e) => {+ eprintln!("{}", e);+ std::process::exit(1);+ }+ };let (entry_id, entry_name, entry_url, entry_format) =- match marketplace::find(id) {- Some(e) => (e.id.to_string(), e.name.to_string(), e.url.to_string(), e.format),- None => {- let community = match marketplace::load_community_packs().await {- Ok((packs, _, _)) => packs.into_iter().find(|p| p.id == id),- Err(_) => None,- };- match community.and_then(|p| {- marketplace::parse_format(&p.format)- .map(|f| (p.id, p.name, p.url, f))- }) {- Some(t) => t,- None => {- eprintln!(- "Unknown ontology ID: '{}'. Run 'marketplace list' to see curated and community IDs.",- id- );- std::process::exit(1);- }- }- }- };+ (pack.id, pack.name, pack.url, pack.format);let (_db, graph) = setup(&cli.data_dir)?;let content = GraphStore::fetch_url(&entry_url).await?;match graph.load_content_with_base(&content, entry_format, Some(&entry_url)) {@@ -2330,3 +2630,169 @@ async fn async_main() -> anyhow::Result<()> {Ok(())}++#[cfg(test)]+mod proxy_serialization_tests {+ use super::*;++ fn args_of(cmd: &Commands) -> (String, Vec<String>) {+ let v = cmd.to_batch_command().expect("command is proxy-able");+ let name = v["command"].as_str().unwrap().to_string();+ let args = v["args"]+ .as_array()+ .unwrap()+ .iter()+ .map(|a| a.as_str().unwrap().to_string())+ .collect();+ (name, args)+ }++ #[test]+ fn a_multi_line_query_stays_one_argument() {+ // The bug this replaced: the query was rendered into a command line and+ // the daemon re-tokenized it, but `parse_lines` splits on newlines before+ // it looks at quotes, so a multi-line query — the normal kind — arrived+ // torn across lines and failed as an unterminated quote, while the same+ // query run locally succeeded.+ let query = "SELECT ?s ?o\nWHERE {\n ?s <http://example.org/p> ?o .\n}";+ let (name, args) = args_of(&Commands::Query { query: query.to_string() });+ assert_eq!(name, "query");+ assert_eq!(args, vec![query.to_string()]);+ }++ #[test]+ fn an_argument_carrying_both_quote_styles_survives_verbatim() {+ // The old double-quote fallback did not escape backslashes, so an+ // argument holding both quote styles came out mangled.+ let query = r#"SELECT ?s WHERE { ?s ?p "it's \"quoted\"" }"#;+ let (_, args) = args_of(&Commands::Query { query: query.to_string() });+ assert_eq!(args, vec![query.to_string()]);+ }++ #[test]+ fn a_relative_path_is_resolved_before_it_leaves_the_caller() {+ // The daemon inherits whatever directory `daemon start` ran in, so a+ // relative path forwarded verbatim resolved against the wrong place.+ let (name, args) = args_of(&Commands::Load { path: "./data/x.ttl".into() });+ assert_eq!(name, "load");+ let sent = std::path::Path::new(&args[0]);+ assert!(sent.is_absolute(), "sent: {}", args[0]);+ assert!(args[0].ends_with("x.ttl"), "sent: {}", args[0]);+ assert!(sent.starts_with(std::env::current_dir().unwrap()));+ }++ #[test]+ fn an_absolute_path_is_left_alone() {+ let abs = if cfg!(windows) { r"C:\onto\proposed.ttl" } else { "/onto/proposed.ttl" };+ let (_, args) = args_of(&Commands::Plan { file: abs.into() });+ assert_eq!(args, vec![abs.to_string()]);+ }++ #[test]+ fn push_serializes_under_the_name_the_batch_runner_answers_to() {+ // `push` was serialized by the proxy with no arm on the other side, so it+ // was the one proxy-able command that could not run while a daemon was up.+ let (name, args) = args_of(&Commands::Push {+ endpoint: "http://example.org/sparql".into(),+ graph: Some("g1".into()),+ });+ assert_eq!(name, "push");+ assert_eq!(args, vec!["http://example.org/sparql", "--graph", "g1"]);+ }++ #[test]+ fn a_flag_the_batch_handler_cannot_honour_keeps_the_command_local() {+ // The batch ingester has no `--format`, so proxying would have dropped it+ // in silence. Running locally honours it.+ let with_format = Commands::Ingest {+ path: "x.csv".into(),+ format: Some("csv".into()),+ mapping: None,+ base_iri: None,+ };+ assert!(with_format.to_batch_command().is_none());++ let without = Commands::Ingest {+ path: "x.csv".into(),+ format: None,+ mapping: None,+ base_iri: None,+ };+ assert!(without.to_batch_command().is_some());+ }++ /// One instance of every variant `to_batch_command` will proxy. If a new+ /// proxy-able command is added and not listed here it simply goes uncovered;+ /// nothing here passes falsely because of it.+ fn every_proxy_able_command() -> Vec<Commands> {+ vec![+ Commands::Load { path: "x.ttl".into() },+ Commands::Save { path: "x.ttl".into(), format: "turtle".into() },+ Commands::Clear,+ Commands::Stats,+ Commands::Query { query: "SELECT ?s WHERE { ?s ?p ?o }".into() },+ Commands::Lint { input: "x.ttl".into() },+ Commands::Reason { profile: "rdfs".into() },+ Commands::Shacl { shapes: "s.ttl".into() },+ Commands::Status,+ Commands::Pull { url: "http://example.org".into(), sparql: false, query: None },+ Commands::Push { endpoint: "http://example.org".into(), graph: None },+ Commands::Version { label: "v1".into() },+ Commands::History,+ Commands::Rollback { label: "v1".into() },+ Commands::Ingest { path: "x.csv".into(), format: None, mapping: None, base_iri: None },+ Commands::Plan { file: "p.ttl".into() },+ Commands::Apply { mode: "safe".into(), plan_id: None },+ Commands::Enforce { pack: "generic".into() },+ Commands::Monitor,+ Commands::MonitorClear,+ Commands::Drift { file_a: "a.ttl".into(), file_b: "b.ttl".into() },+ Commands::Lock { iris: vec!["http://example.org/A".into()], reason: None },+ Commands::Marketplace { action: "list".into(), id: None, domain: None },+ ]+ }++ /// The names on the two sides of the proxy are matched by string across an+ /// HTTP boundary, and nothing but this checks that they agree. `push` was+ /// serialized here with no arm in the batch runner, so it was the one+ /// proxy-able command that could not run while a daemon was up: it fell+ /// through to `unknown batch command` and exited 1.+ ///+ /// Every command is invoked with no arguments, which each arm rejects before+ /// doing anything — the network ones included — so what this asserts is that+ /// the arm exists, not what it does.+ #[tokio::test]+ async fn every_proxy_able_command_has_an_arm_in_the_batch_runner() {+ let dir = tempfile::tempdir().unwrap();+ let db = StateDb::open(&dir.path().join("state.db")).unwrap();+ let runner = open_ontologies::batch::BatchRunner::new(+ db,+ Arc::new(GraphStore::new()),+ false,+ );++ for cmd in every_proxy_able_command() {+ let Some(v) = cmd.to_batch_command() else {+ panic!("expected a proxy-able command");+ };+ let name = v["command"].as_str().unwrap().to_string();+ let payload = serde_json::to_string(&vec![+ serde_json::json!({"command": name, "args": []}),+ ])+ .unwrap();+ let (results, _) = runner.run_collect(&payload, false).await;+ let err = results[0]["result"]["error"].as_str().unwrap_or("");+ assert!(+ !err.contains("unknown batch command"),+ "`{name}` is proxied but the batch runner has no arm for it"+ );+ }+ }++ #[test]+ fn commands_that_must_run_locally_are_not_proxied() {+ assert!(Commands::Diff { old_path: "a.ttl".into(), new_path: "b.ttl".into() }+ .to_batch_command()+ .is_none());+ }+}diff --git a/src/marketplace.rs b/src/marketplace.rsindex 6d864b84..04fdab87 100644--- a/src/marketplace.rs+++ b/src/marketplace.rs@@ -459,6 +459,100 @@ pub fn parse_community_registry(json: &str) -> Result<(Vec<CommunityEntry>, Vec<Ok((packs, shadowed))}+// ─── Shared CLI surface ──────────────────────────────────────────────+//+// `marketplace list` and `marketplace install` are one user-facing command with+// two implementations behind it: the local one in `main.rs` and the batch one in+// `batch.rs` that serves it whenever a daemon is running. The two had drifted —+// the batch copy consulted only the curated catalogue and never loaded community+// packs — so the same command answered with a different catalogue depending on+// whether a daemon happened to be up, and said nothing about the difference.+// Both now go through the functions below.+//+// The MCP tool in `server.rs` deliberately reports a richer shape (urls,+// maintainer, shadowing warnings) and is left as its own surface.++/// One pack resolved for installation, from either tier.+pub struct ResolvedPack {+ pub id: String,+ pub name: String,+ pub url: String,+ pub format: RdfFormat,+}++/// The catalogue as the CLI reports it: curated entries first, then community+/// packs, which can never shadow a curated id. The second element is a message+/// about the community registry when it could not be loaded, which is reported+/// rather than being allowed to fail the listing.+pub async fn cli_list(domain: Option<&str>) -> (Vec<serde_json::Value>, Option<String>) {+ let mut items: Vec<serde_json::Value> = list(domain)+ .iter()+ .map(|e| {+ serde_json::json!({+ "id": e.id,+ "name": e.name,+ "description": e.description,+ "domain": e.domain,+ "format": format_name(e.format),+ "source": "curated",+ })+ })+ .collect();++ let mut community_error = None;+ match load_community_packs().await {+ Ok((packs, _shadowed, _source)) => {+ for p in packs+ .iter()+ .filter(|p| domain.is_none_or(|d| p.domain == d))+ {+ items.push(serde_json::json!({+ "id": p.id,+ "name": p.name,+ "description": p.description,+ "domain": p.domain,+ "format": p.format,+ "source": "community",+ }));+ }+ }+ Err(e) => community_error = Some(e),+ }+ (items, community_error)+}++/// Resolve an install id across both tiers, curated first. The error is the+/// message to show the user.+pub async fn cli_resolve(id: &str) -> Result<ResolvedPack, String> {+ if let Some(e) = find(id) {+ return Ok(ResolvedPack {+ id: e.id.to_string(),+ name: e.name.to_string(),+ url: e.url.to_string(),+ format: e.format,+ });+ }+ let community = match load_community_packs().await {+ Ok((packs, _, _)) => packs.into_iter().find(|p| p.id == id),+ Err(_) => None,+ };+ match community.and_then(|p| {+ parse_format(&p.format).map(|f| ResolvedPack {+ id: p.id,+ name: p.name,+ url: p.url,+ format: f,+ })+ }) {+ Some(r) => Ok(r),+ None => Err(format!(+ "Unknown ontology ID: '{}'. Run 'marketplace list' to see curated and community IDs.",+ id+ )),+ }+}++#[cfg(test)]mod tests {use super::*;diff --git a/src/output.rs b/src/output.rsnew file mode 100644index 00000000..c0046ecd--- /dev/null+++ b/src/output.rs@@ -0,0 +1,374 @@+//! Human-readable rendering of JSON result values for CLI output.++use serde_json::Value;++/// Render a result whose producing command is known, dispatching on the command+/// rather than guessing from which keys the payload happens to carry.+///+/// Key-sniffing is what made a successful `daemon start` print "Daemon dead":+/// `{ok, pid, url}` matched a branch meant for `daemon status`. The batch+/// envelope names the command that produced each result, so on the proxy path+/// there is no need to guess at all. `None` falls back to sniffing, which is+/// still what the local one-shot paths use.+pub fn render_human_for(command: Option<&str>, value: &Value) -> String {+ if let Some(msg) = value.get("error").and_then(|v| v.as_str()) {+ return format!("Error: {}", msg);+ }+ match command {+ Some("query") => {+ let bindings = value+ .get("results")+ .and_then(|r| r.get("bindings"))+ .and_then(|b| b.as_array());+ match bindings {+ Some(b) => {+ let vars: Vec<&str> = value+ .get("variables")+ .and_then(|v| v.as_array())+ .map(|a| a.iter().filter_map(|v| v.as_str()).collect())+ .unwrap_or_default();+ render_sparql_table(&vars, b)+ }+ None => render_human(value),+ }+ }+ Some("stats") => render_stats(value),+ Some("marketplace") => match value.get("ontologies").and_then(|v| v.as_array()) {+ Some(list) => render_marketplace_list(list),+ None => render_human(value),+ },+ _ => render_human(value),+ }+}++/// Render a JSON result value as human-readable text.+/// Falls back to pretty-printed JSON for shapes that have no specific rendering.+pub fn render_human(value: &Value) -> String {+ // Error+ if let Some(msg) = value.get("error").and_then(|v| v.as_str()) {+ return format!("Error: {}", msg);+ }++ // SPARQL query results+ if let Some(bindings) = value+ .get("results")+ .and_then(|r| r.get("bindings"))+ .and_then(|b| b.as_array())+ {+ let vars: Vec<&str> = value+ .get("variables")+ .and_then(|v| v.as_array())+ .map(|a| a.iter().filter_map(|v| v.as_str()).collect())+ .unwrap_or_default();+ return render_sparql_table(&vars, bindings);+ }++ // Stats shape: has "triples" field+ if value.get("triples").is_some() {+ return render_stats(value);+ }++ // Marketplace list+ if let Some(ontologies) = value.get("ontologies").and_then(|v| v.as_array()) {+ return render_marketplace_list(ontologies);+ }++ // Versions / history list+ if let Some(versions) = value.get("versions").and_then(|v| v.as_array()) {+ if versions.is_empty() {+ return "No saved versions.".into();+ }+ let mut out = String::from("Saved versions:\n");+ for v in versions {+ let label = v.get("label").and_then(|l| l.as_str()).unwrap_or("?");+ let ts = v.get("created_at").and_then(|t| t.as_str()).unwrap_or("");+ out.push_str(&format!(" {}", label));+ if !ts.is_empty() {+ out.push_str(&format!(" ({})", ts));+ }+ out.push('\n');+ }+ return out.trim_end().into();+ }++ // Lint issues+ if let Some(issues) = value.get("issues").and_then(|v| v.as_array()) {+ if issues.is_empty() {+ return "No issues found.".into();+ }+ let mut out = format!("{} issue(s):\n", issues.len());+ for issue in issues {+ let sev = issue.get("severity").and_then(|s| s.as_str()).unwrap_or("warning");+ let msg = issue.get("message").and_then(|m| m.as_str()).unwrap_or("?");+ out.push_str(&format!(" [{}] {}\n", sev, msg));+ }+ return out.trim_end().into();+ }++ // Enforce violations+ if let Some(violations) = value.get("violations").and_then(|v| v.as_array()) {+ if violations.is_empty() {+ return "No violations found.".into();+ }+ let mut out = format!("{} violation(s):\n", violations.len());+ for v in violations {+ let rule = v.get("rule").and_then(|r| r.as_str()).unwrap_or("?");+ let msg = v.get("message").and_then(|m| m.as_str()).unwrap_or("?");+ out.push_str(&format!(" [{}] {}\n", rule, msg));+ }+ return out.trim_end().into();+ }++ // Daemon status. Gated on an explicit `alive`, not merely on `pid` + `url`:+ // a successful `daemon start` answers {ok, pid, url}, which matched this+ // branch before the `ok` branch below and reported the daemon it had just+ // started as "Daemon dead". Only `daemon status` computes liveness, so only+ // `daemon status` should be rendered as liveness.+ if let (Some(pid), Some(url), Some(alive)) = (+ value.get("pid"),+ value.get("url"),+ value.get("alive").and_then(|a| a.as_bool()),+ ) {+ let status = if alive { "running" } else { "dead" };+ return format!(+ "Daemon {} — PID {} at {}",+ status,+ pid,+ url.as_str().unwrap_or("?")+ );+ }++ // Ok + specific known messages+ if value.get("ok").and_then(|v| v.as_bool()).unwrap_or(false) {+ // marketplace install: {"ok":true, "installed":"...", "name":"...", "triples_loaded":N}+ if let Some(name) = value.get("name").and_then(|v| v.as_str()) {+ let n = value+ .get("triples_loaded")+ .and_then(|v| v.as_u64())+ .unwrap_or(0);+ return format!("Installed \"{}\": {} triples loaded.", name, n);+ }++ // load: {"ok":true, "triples_loaded":N, "path":"..."}+ if let Some(n) = value.get("triples_loaded").and_then(|v| v.as_u64()) {+ if let Some(path) = value.get("path").and_then(|v| v.as_str()) {+ return format!("Loaded {} triples from {}.", n, path);+ }+ if let Some(src) = value.get("source").and_then(|v| v.as_str()) {+ return format!("Pulled {} triples from {}.", n, src);+ }+ return format!("Loaded {} triples.", n);+ }++ // save: {"ok":true, "path":"...", "format":"..."}+ if let (Some(path), Some(fmt)) = (+ value.get("path").and_then(|v| v.as_str()),+ value.get("format").and_then(|v| v.as_str()),+ ) {+ return format!("Saved to {} (format: {}).", path, fmt);+ }++ // daemon start: {"ok":true, "pid":N, "url":"..."}+ if let (Some(pid), Some(url)) = (+ value.get("pid"),+ value.get("url").and_then(|v| v.as_str()),+ ) {+ return format!("Daemon started — PID {} at {}", pid, url);+ }++ // message only: {"ok":true, "message":"..."}+ if let Some(msg) = value.get("message").and_then(|v| v.as_str()) {+ return msg.to_string();+ }++ // plan result+ if let Some(risk) = value.get("risk_score").and_then(|v| v.as_str()) {+ let added = value+ .get("added_classes")+ .and_then(|v| v.as_array())+ .map(|a| a.len())+ .unwrap_or(0);+ let removed = value+ .get("removed_classes")+ .and_then(|v| v.as_array())+ .map(|a| a.len())+ .unwrap_or(0);+ return format!(+ "Plan: risk={}, +{} classes, -{} classes.",+ risk, added, removed+ );+ }++ return "OK".into();+ }++ // Status command: {"status":"ok","version":"...","triples_loaded":N}+ if let Some(status) = value.get("status").and_then(|v| v.as_str()) {+ let version = value+ .get("version")+ .and_then(|v| v.as_str())+ .unwrap_or("?");+ let triples = value+ .get("triples_loaded")+ .and_then(|v| v.as_u64())+ .unwrap_or(0);+ return format!(+ "Status: {} (v{}, {} triples in store)",+ status, version, triples+ );+ }++ // Diff / drift summary+ if let Some(added) = value.get("added") {+ let removed = value.get("removed");+ let a = added.as_array().map(|a| a.len()).unwrap_or(0);+ let r = removed+ .and_then(|v| v.as_array())+ .map(|a| a.len())+ .unwrap_or(0);+ return format!("Diff: +{} triples, -{} triples.", a, r);+ }++ // Count-only results: {"count":N}+ if let (Some(count), 1) = (value.get("count").and_then(|v| v.as_u64()), value.as_object().map(|o| o.len()).unwrap_or(0)) {+ return format!("{} item(s).", count);+ }++ // Fallback: pretty-print JSON+ serde_json::to_string_pretty(value).unwrap_or_else(|_| value.to_string())+}++fn render_stats(value: &Value) -> String {+ let triples = value.get("triples").and_then(|v| v.as_u64()).unwrap_or(0);+ let classes = value.get("classes").and_then(|v| v.as_u64()).unwrap_or(0);+ let props = value.get("properties").and_then(|v| v.as_u64()).unwrap_or(0);+ let individuals = value.get("individuals").and_then(|v| v.as_u64()).unwrap_or(0);+ format!(+ "Triples: {}\nClasses: {}\nProperties: {}\nIndividuals: {}",+ triples, classes, props, individuals+ )+}++fn render_sparql_table(vars: &[&str], bindings: &[Value]) -> String {+ if bindings.is_empty() {+ return "No results.".into();+ }+ // Collect column widths+ let mut widths: Vec<usize> = vars.iter().map(|v| v.len()).collect();+ let rows: Vec<Vec<String>> = bindings+ .iter()+ .map(|row| {+ vars.iter()+ .enumerate()+ .map(|(i, var)| {+ let cell = row+ .get(var)+ .and_then(|v| {+ v.get("value")+ .and_then(|v| v.as_str())+ .map(|s| s.to_string())+ })+ .unwrap_or_default();+ if cell.len() > widths[i] {+ widths[i] = cell.len();+ }+ cell+ })+ .collect()+ })+ .collect();++ let mut out = String::new();+ // Header+ let header: Vec<String> = vars+ .iter()+ .enumerate()+ .map(|(i, v)| format!("{:<width$}", v, width = widths[i]))+ .collect();+ out.push_str(&header.join(" "));+ out.push('\n');+ // Separator+ let sep: Vec<String> = widths.iter().map(|&w| "-".repeat(w)).collect();+ out.push_str(&sep.join(" "));+ out.push('\n');+ // Rows+ for row in &rows {+ let cells: Vec<String> = row+ .iter()+ .enumerate()+ .map(|(i, cell)| format!("{:<width$}", cell, width = widths[i]))+ .collect();+ out.push_str(&cells.join(" "));+ out.push('\n');+ }+ out.trim_end().into()+}++fn render_marketplace_list(ontologies: &[Value]) -> String {+ if ontologies.is_empty() {+ return "No ontologies found.".into();+ }+ let mut out = String::new();+ for o in ontologies {+ let id = o.get("id").and_then(|v| v.as_str()).unwrap_or("?");+ let name = o.get("name").and_then(|v| v.as_str()).unwrap_or("?");+ let desc = o.get("description").and_then(|v| v.as_str()).unwrap_or("");+ let domain = o.get("domain").and_then(|v| v.as_str()).unwrap_or("");+ out.push_str(&format!(" {:20} {} [{}]\n", id, name, domain));+ if !desc.is_empty() {+ out.push_str(&format!(" {}\n", desc));+ }+ }+ out.trim_end().into()+}++#[cfg(test)]+mod tests {+ use super::*;+ use serde_json::json;++ #[test]+ fn a_started_daemon_is_not_reported_as_dead() {+ // The regression this exists for: `daemon start` answers {ok, pid, url},+ // which matched the daemon-status branch — a branch that defaults+ // liveness to false — and printed "Daemon dead" for a daemon that had+ // just come up.+ let started = json!({"ok": true, "pid": 4242, "url": "http://127.0.0.1:8080"});+ let rendered = render_human(&started);+ assert!(!rendered.contains("dead"), "got: {rendered}");+ assert!(rendered.contains("4242"), "got: {rendered}");+ }++ #[test]+ fn daemon_status_still_reports_liveness_both_ways() {+ let alive = json!({"pid": 1, "url": "http://127.0.0.1:8080", "alive": true});+ assert!(render_human(&alive).contains("running"));+ let dead = json!({"pid": 1, "url": "http://127.0.0.1:8080", "alive": false});+ assert!(render_human(&dead).contains("dead"));+ }++ #[test]+ fn a_known_command_dispatches_without_sniffing_keys() {+ // `stats` renders as stats because the envelope said the command was+ // `stats`, not because the payload happened to carry a `triples` key.+ let payload = json!({"triples": 12});+ assert_eq!(+ render_human_for(Some("stats"), &payload),+ render_stats(&payload)+ );+ }++ #[test]+ fn an_error_is_an_error_whatever_produced_it() {+ let payload = json!({"error": "no such file"});+ assert_eq!(render_human_for(Some("load"), &payload), "Error: no such file");+ assert_eq!(render_human_for(None, &payload), "Error: no such file");+ }++ #[test]+ fn an_unknown_command_falls_back_to_the_shape() {+ let payload = json!({"ok": true, "triples_loaded": 3, "path": "/tmp/x.ttl"});+ assert_eq!(render_human_for(None, &payload), render_human(&payload));+ assert!(render_human_for(Some("load"), &payload).contains("3 triples"));+ }+}