Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

feat(storage): opt-in persistent Oxigraph triple store

Ladislav Gazo36f334f2fc41
diff --git a/src/config.rs b/src/config.rs
index 0296ec5c..06200284 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -10,6 +10,7 @@ pub struct Config {
pub embeddings: EmbeddingsConfig,
pub language: LanguageConfig,
pub cache: CacheConfig,
+ pub storage: StorageConfig,
pub tools: ToolsConfig,
pub webhook: WebhookConfig,
pub http: HttpConfig,
@@ -286,6 +287,63 @@ impl Default for CacheConfig {
}
}
+/// `[storage]` — backend for the main triple store.
+///
+/// `"memory"` (default) keeps the active ontology in RAM only — fast, but
+/// every restart starts empty. `"persistent"` opens a RocksDB-backed
+/// Oxigraph store at `<data_dir>/triplestore`, so triples survive restarts
+/// and the one-shot CLI subcommands (`load` then `query`) share state.
+///
+/// Override the mode at runtime with `OPEN_ONTOLOGIES_STORAGE_MODE`.
+///
+/// Oxigraph allows only one read-write handle on a persistent store at a
+/// time — running two server processes against the same `data_dir` will
+/// fail to open the second store.
+#[derive(Debug, Deserialize, Clone)]
+#[serde(default)]
+pub struct StorageConfig {
+ /// `"memory"` or `"persistent"`. Default `"memory"`.
+ pub mode: String,
+}
+
+impl Default for StorageConfig {
+ fn default() -> Self {
+ Self {
+ mode: "memory".into(),
+ }
+ }
+}
+
+/// Storage backend selected for the main graph.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum StorageMode {
+ Memory,
+ Persistent,
+}
+
+/// Resolve the storage mode.
+///
+/// Precedence: `OPEN_ONTOLOGIES_STORAGE_MODE` env var > config > default
+/// (`memory`). Unknown values fall back to `memory` with a warning printed to
+/// stderr — preserves the historical in-memory behaviour for typos.
+pub fn resolve_storage_mode(cfg: &StorageConfig) -> StorageMode {
+ let raw = std::env::var("OPEN_ONTOLOGIES_STORAGE_MODE")
+ .ok()
+ .filter(|v| !v.trim().is_empty())
+ .unwrap_or_else(|| cfg.mode.clone());
+ match raw.trim().to_lowercase().as_str() {
+ "memory" | "mem" | "in-memory" | "" => StorageMode::Memory,
+ "persistent" | "disk" | "rocksdb" => StorageMode::Persistent,
+ other => {
+ eprintln!(
+ "warning: unknown [storage] mode {:?}; falling back to \"memory\"",
+ other
+ );
+ StorageMode::Memory
+ }
+ }
+}
+
/// Configuration for limiting which MCP tools are exposed.
#[derive(Debug, Deserialize, Clone, Default)]
#[serde(default)]
diff --git a/src/graph.rs b/src/graph.rs
index dc648cd0..0f780fb6 100644
--- a/src/graph.rs
+++ b/src/graph.rs
@@ -1,4 +1,5 @@
use std::io::Cursor;
+use std::path::Path;
use std::sync::Mutex;
use oxigraph::io::{RdfFormat, RdfParser, RdfSerializer};
@@ -66,6 +67,30 @@ impl GraphStore {
}
}
+ /// Open a RocksDB-backed persistent store at `path`, creating it if missing.
+ ///
+ /// Oxigraph allows only one read-write handle per directory; opening the
+ /// same path from a second process will fail. Sandbox stores throughout
+ /// the codebase keep using [`GraphStore::new`] — only the main graph
+ /// should ever be persistent.
+ pub fn open_persistent(path: &Path) -> anyhow::Result<Self> {
+ std::fs::create_dir_all(path).map_err(|e| {
+ anyhow::anyhow!(
+ "failed to create persistent triplestore directory {}: {e}",
+ path.display()
+ )
+ })?;
+ let store = Store::open(path).map_err(|e| {
+ anyhow::anyhow!(
+ "failed to open persistent Oxigraph store at {}: {e}",
+ path.display()
+ )
+ })?;
+ Ok(Self {
+ store: Mutex::new(store),
+ })
+ }
+
pub fn triple_count(&self) -> usize {
let store = self.store.lock().unwrap();
store.len().unwrap_or(0)
diff --git a/src/main.rs b/src/main.rs
index cb6ac222..28e29979 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -37,6 +37,14 @@ data_dir = "~/.open-ontologies"
# # cache fingerprint tie-breaker. Increase for very large dumps.
# hash_prefix_bytes = 65536
+# [storage]
+# Backend for the main triple store.
+# mode = "memory" # in-memory (default; lost on restart)
+# mode = "persistent" # RocksDB at <data_dir>/triplestore; survives restarts
+# Override at runtime with OPEN_ONTOLOGIES_STORAGE_MODE or `--storage-mode`.
+# Note: only one server process can hold the persistent store open at a time.
+# mode = "memory"
+
# [tools]
# Restrict which MCP tools are exposed by this server.
# mode = "all" | "allow" | "deny"
@@ -206,6 +214,11 @@ enum Commands {
/// When set, every read tool checks the source file for changes and recompiles.
#[arg(long)]
auto_refresh: bool,
+ /// Storage backend for the main triple store: `memory` (default,
+ /// in-memory) or `persistent` (RocksDB at `<data_dir>/triplestore`).
+ /// CLI > `OPEN_ONTOLOGIES_STORAGE_MODE` env > `[storage] mode`.
+ #[arg(long)]
+ storage_mode: Option<String>,
},
/// Start the MCP server (Streamable HTTP transport)
ServeHttp {
@@ -246,6 +259,11 @@ enum Commands {
/// When set, every read tool checks the source file for changes and recompiles.
#[arg(long)]
auto_refresh: bool,
+ /// Storage backend for the main triple store: `memory` (default,
+ /// in-memory) or `persistent` (RocksDB at `<data_dir>/triplestore`).
+ /// CLI > `OPEN_ONTOLOGIES_STORAGE_MODE` env > `[storage] mode`.
+ #[arg(long)]
+ storage_mode: Option<String>,
},
/// Start unix socket server for Tardygrada fact grounding
@@ -542,10 +560,43 @@ fn setup(data_dir: &str) -> anyhow::Result<(StateDb, Arc<GraphStore>)> {
std::fs::create_dir_all(data_path)?;
let db_path = data_path.join("open-ontologies.db");
let db = StateDb::open(&db_path)?;
- let graph = Arc::new(GraphStore::new());
+ // One-shot CLI subcommands respect the same [storage] setting as the
+ // server modes (loaded from `<data_dir>/config.toml` if present). This is
+ // what makes `open-ontologies load foo.ttl` + `open-ontologies query ...`
+ // share state when persistence is enabled.
+ let cfg_path = data_path.join("config.toml");
+ let storage_cfg = open_ontologies::config::Config::load(&cfg_path)
+ .map(|c| c.storage)
+ .unwrap_or_default();
+ let graph = build_main_graph(&storage_cfg, data_path)?;
Ok((db, graph))
}
+/// Build the singleton main graph using the configured storage backend.
+///
+/// In-memory: returns an empty `GraphStore`.
+/// Persistent: opens (or creates) a RocksDB-backed store at
+/// `<data_dir>/triplestore`.
+fn build_main_graph(
+ cfg: &open_ontologies::config::StorageConfig,
+ data_dir: &std::path::Path,
+) -> anyhow::Result<Arc<GraphStore>> {
+ use open_ontologies::config::{resolve_storage_mode, StorageMode};
+ match resolve_storage_mode(cfg) {
+ StorageMode::Memory => Ok(Arc::new(GraphStore::new())),
+ StorageMode::Persistent => {
+ let path = data_dir.join("triplestore");
+ let store = GraphStore::open_persistent(&path)?;
+ tracing::info!(
+ "opened persistent triple store at {} ({} triples)",
+ path.display(),
+ store.triple_count()
+ );
+ Ok(Arc::new(store))
+ }
+ }
+}
+
fn output_json(value: &serde_json::Value, pretty: bool) {
if pretty {
println!("{}", serde_json::to_string_pretty(value).unwrap());
@@ -808,6 +859,7 @@ async fn async_main() -> anyhow::Result<()> {
tools_deny,
idle_ttl_secs,
auto_refresh,
+ storage_mode,
} => {
let config_path = expand_tilde(&config_path);
let cfg = match Config::load(std::path::Path::new(&config_path)) {
@@ -828,12 +880,17 @@ async fn async_main() -> anyhow::Result<()> {
open_ontologies::runtime::init_from_config(&cfg);
let data_dir = expand_tilde(&cfg.general.data_dir);
- let db_path = std::path::Path::new(&data_dir).join("open-ontologies.db");
+ let data_path = std::path::Path::new(&data_dir);
+ let db_path = data_path.join("open-ontologies.db");
- std::fs::create_dir_all(&data_dir)?;
+ std::fs::create_dir_all(data_path)?;
let db = StateDb::open(&db_path)?;
- let graph = Arc::new(GraphStore::new());
+ let mut storage_cfg = cfg.storage.clone();
+ if let Some(m) = storage_mode.as_deref() {
+ storage_cfg.mode = m.to_string();
+ }
+ let graph = build_main_graph(&storage_cfg, data_path)?;
// Monitor: CLI `--watch` forces enabled; otherwise fall back to
// `[monitor] enabled`. CLI `--watch-interval` > env > `[monitor]
@@ -892,6 +949,7 @@ async fn async_main() -> anyhow::Result<()> {
tools_deny,
idle_ttl_secs,
auto_refresh,
+ storage_mode,
} => {
use rmcp::transport::streamable_http_server::{
StreamableHttpServerConfig, StreamableHttpService,
@@ -927,12 +985,17 @@ async fn async_main() -> anyhow::Result<()> {
let token = token.or_else(|| open_ontologies::config::resolve_http_token(&cfg.http));
let data_dir = expand_tilde(&cfg.general.data_dir);
- let db_path_owned = std::path::Path::new(&data_dir).join("open-ontologies.db");
+ let data_path_owned = std::path::PathBuf::from(&data_dir);
+ let db_path_owned = data_path_owned.join("open-ontologies.db");
- std::fs::create_dir_all(&data_dir)?;
+ std::fs::create_dir_all(&data_path_owned)?;
// Shared graph store — all MCP sessions (agent + frontend) see the same triples
- let shared_graph = Arc::new(GraphStore::new());
+ let mut storage_cfg = cfg.storage.clone();
+ if let Some(m) = storage_mode.as_deref() {
+ storage_cfg.mode = m.to_string();
+ }
+ let shared_graph = build_main_graph(&storage_cfg, &data_path_owned)?;
// Shared StateDb for lineage REST endpoint
let shared_db = StateDb::open(&db_path_owned)?;
diff --git a/tests/persistent_store_test.rs b/tests/persistent_store_test.rs
new file mode 100644
index 00000000..382303c4
--- /dev/null
+++ b/tests/persistent_store_test.rs
@@ -0,0 +1,55 @@
+use open_ontologies::config::{resolve_storage_mode, StorageConfig, StorageMode};
+use open_ontologies::graph::GraphStore;
+use tempfile::TempDir;
+
+#[test]
+fn persistent_store_survives_drop_and_reopen() {
+ let dir = TempDir::new().unwrap();
+ let path = dir.path().join("triplestore");
+
+ let ttl = r#"
+ @prefix ex: <http://example.org/> .
+ ex:Alice a ex:Person .
+ ex:Bob a ex:Person .
+ "#;
+
+ {
+ let store = GraphStore::open_persistent(&path).unwrap();
+ assert_eq!(store.triple_count(), 0);
+ store.load_turtle(ttl, None).unwrap();
+ assert_eq!(store.triple_count(), 2);
+ }
+
+ {
+ let store = GraphStore::open_persistent(&path).unwrap();
+ assert_eq!(store.triple_count(), 2);
+ let json = store
+ .sparql_select("SELECT ?s WHERE { ?s a <http://example.org/Person> }")
+ .unwrap();
+ assert!(json.contains("Alice"));
+ assert!(json.contains("Bob"));
+ }
+}
+
+#[test]
+fn storage_mode_resolves_default_to_memory() {
+ let cfg = StorageConfig::default();
+ // Guard against a stray env var in the test runner shell.
+ // SAFETY: tests run single-threaded for env mutation by default in cargo;
+ // this is only touched here, so no cross-test interference.
+ unsafe {
+ std::env::remove_var("OPEN_ONTOLOGIES_STORAGE_MODE");
+ }
+ assert_eq!(resolve_storage_mode(&cfg), StorageMode::Memory);
+}
+
+#[test]
+fn storage_mode_parses_persistent() {
+ let cfg = StorageConfig {
+ mode: "persistent".to_string(),
+ };
+ unsafe {
+ std::env::remove_var("OPEN_ONTOLOGIES_STORAGE_MODE");
+ }
+ assert_eq!(resolve_storage_mode(&cfg), StorageMode::Persistent);
+}