| Crates.io | ruv-swarm-persistence |
| lib.rs | ruv-swarm-persistence |
| version | 1.0.5 |
| created_at | 2025-06-30 04:37:38.191681+00 |
| updated_at | 2025-07-02 20:37:26.416697+00 |
| description | Persistence layer with SQLite and ORM for RUV-Swarm |
| homepage | |
| repository | https://github.com/ruvnet/ruv-FANN |
| max_upload_size | |
| id | 1731412 |
| size | 193,236 |
High-performance, ACID-compliant persistence layer for RUV-Swarm with SQLite and cross-platform support.
ruv-swarm-persistence is the foundational data persistence layer for the RUV-Swarm distributed agent system. It provides robust, type-safe data storage with full ACID compliance, real-time persistence, and seamless cross-platform compatibility.
Built with modern Rust principles, this crate delivers memory-safe database operations, connection pooling, transaction support, and a flexible repository pattern for managing swarm agent state, task coordination, event sourcing, and performance metrics.
Add to your Cargo.toml:
[dependencies]
ruv-swarm-persistence = "0.1.0"
# For WASM support
[target.'cfg(target_arch = "wasm32")'.dependencies]
ruv-swarm-persistence = { version = "0.1.0", features = ["wasm"] }
use ruv_swarm_persistence::{init_storage, AgentModel, TaskModel, Storage};
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize storage (SQLite on native, IndexedDB on WASM)
let storage = init_storage(Some("swarm.db")).await?;
// Create and store an agent
let mut agent = AgentModel::new(
"worker-001".to_string(),
"researcher".to_string(),
vec!["analysis".to_string(), "data-processing".to_string()]
);
storage.store_agent(&agent).await?;
// Retrieve agent
let retrieved = storage.get_agent(&agent.id).await?;
println!("Agent retrieved: {:?}", retrieved);
Ok(())
}
use ruv_swarm_persistence::{SqliteStorage, Storage, TaskModel, TaskPriority};
async fn atomic_task_processing() -> Result<(), Box<dyn std::error::Error>> {
let storage = SqliteStorage::new("swarm.db").await?;
// Begin transaction
let mut tx = storage.begin_transaction().await?;
// Multiple operations in single transaction
let task1 = TaskModel::new(
"analysis".to_string(),
serde_json::json!({"dataset": "sales_data.csv"}),
TaskPriority::High
);
let task2 = TaskModel::new(
"report".to_string(),
serde_json::json!({"template": "quarterly"}),
TaskPriority::Medium
);
// Both tasks stored atomically
storage.store_task(&task1).await?;
storage.store_task(&task2).await?;
// Commit all changes
tx.commit().await?;
Ok(())
}
use ruv_swarm_persistence::{QueryBuilder, AgentModel};
// Type-safe query construction
let query = QueryBuilder::<AgentModel>::new("agents")
.where_eq("status", "active")
.where_like("agent_type", "research%")
.order_by("created_at", true)
.limit(50)
.build();
println!("Generated SQL: {}", query);
// Output: SELECT * FROM agents WHERE status = 'active' AND agent_type LIKE 'research%' ORDER BY created_at DESC LIMIT 50
use ruv_swarm_persistence::{EventModel, Storage};
async fn track_agent_events(storage: &dyn Storage) -> Result<(), Box<dyn std::error::Error>> {
let event = EventModel::new(
"agent_started".to_string(),
Some("agent-123".to_string()),
serde_json::json!({
"startup_time": "2024-01-01T10:00:00Z",
"capabilities": ["nlp", "analysis"]
})
);
storage.store_event(&event).await?;
// Query recent events
let recent_events = storage.get_events_since(
chrono::Utc::now().timestamp() - 3600 // Last hour
).await?;
println!("Recent events: {}", recent_events.len());
Ok(())
}
use ruv_swarm_persistence::{MetricModel, Storage};
async fn performance_monitoring(storage: &dyn Storage) -> Result<(), Box<dyn std::error::Error>> {
// Store performance metrics
let cpu_metric = MetricModel::new(
"cpu_usage".to_string(),
Some("agent-456".to_string()),
85.5,
"percent".to_string(),
[("host".to_string(), "worker-node-01".into())].into()
);
storage.store_metric(&cpu_metric).await?;
// Query aggregated metrics
let metrics = storage.get_aggregated_metrics(
"cpu_usage",
chrono::Utc::now().timestamp() - 86400, // Last 24 hours
chrono::Utc::now().timestamp()
).await?;
println!("CPU metrics over 24h: {}", metrics.len());
Ok(())
}
The persistence layer uses a comprehensive schema optimized for swarm operations:
agents - Agent lifecycle and metadata managementtasks - Priority-based task queue with dependenciesevents - Event sourcing for audit trailsmessages - Inter-agent communicationmetrics - Performance and monitoring dataschema_migrations - Version tracking for database evolution-- Performance-optimized indexes
CREATE INDEX idx_agents_status ON agents(status);
CREATE INDEX idx_tasks_priority ON tasks(priority DESC);
CREATE INDEX idx_events_timestamp ON events(timestamp);
CREATE INDEX idx_messages_unread ON messages(to_agent, read);
CREATE INDEX idx_metrics_type_time ON metrics(metric_type, timestamp);
-- Optimized for concurrent access
PRAGMA foreign_keys = ON;
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
use ruv_swarm_persistence::SqliteStorage;
// Custom pool configuration
let storage = SqliteStorage::builder()
.max_connections(32)
.min_idle_connections(4)
.connection_timeout(Duration::from_secs(60))
.idle_timeout(Duration::from_secs(600))
.build("swarm.db")
.await?;
[dependencies]
ruv-swarm-persistence = {
version = "0.1.0",
features = ["wasm", "migrations"]
}
Available features:
wasm - Enable WASM/IndexedDB supportmigrations - Database migration utilitiesrusqlite-backend (default) - Native SQLite supportBenchmarks on Apple M1 Pro with SQLite WAL mode:
| Operation | Throughput | Latency (p99) |
|---|---|---|
| Agent Insert | 15,000 ops/sec | 2.1ms |
| Task Query | 25,000 ops/sec | 1.8ms |
| Event Batch | 30,000 ops/sec | 1.2ms |
| Metric Aggregation | 8,000 ops/sec | 3.5ms |
| Transaction Commit | 5,000 ops/sec | 4.2ms |
Run cargo bench for platform-specific benchmarks
The crate includes comprehensive test coverage:
# Run all tests
cargo test
# Run with coverage
cargo tarpaulin --out html
# Property-based testing
cargo test --features proptest
# WASM testing
wasm-pack test --node
use ruv_swarm_persistence::{Storage, StorageError, AgentModel};
use async_trait::async_trait;
pub struct CustomStorage {
// Your implementation
}
#[async_trait]
impl Storage for CustomStorage {
type Error = StorageError;
async fn store_agent(&self, agent: &AgentModel) -> Result<(), Self::Error> {
// Custom storage logic
Ok(())
}
// Implement other required methods...
}
use ruv_swarm_persistence::migrations::{Migration, MigrationRunner};
let migration = Migration::new(
2,
"add_agent_tags",
"ALTER TABLE agents ADD COLUMN tags TEXT DEFAULT '{}';"
);
let runner = MigrationRunner::new(&storage);
runner.run_migration(migration).await?;
Storage - Main persistence interfaceTransaction - ACID transaction supportRepository<T> - Type-safe data access patternAgentModel - Swarm agent representationTaskModel - Work item with priority and dependenciesEventModel - Event sourcing recordMessageModel - Inter-agent communicationMetricModel - Performance and monitoring dataStorageError - Comprehensive error handlingDatabase - SQL execution errorsSerialization - JSON serialization issuesNotFound - Resource lookup failuresTransaction - ACID transaction errorsWe welcome contributions! Please see our Contributing Guide for details.
git clone https://github.com/ruvnet/ruv-fann.git
cd ruv-fann/ruv-swarm/crates/ruv-swarm-persistence
cargo build
cargo test
This project is licensed under the MIT License - see the LICENSE file for details.
Created by rUv - Building the future of distributed AI systems with Rust ๐ฆ