runtimo_core/lib.rs
1//! Runtimo Core — Agent-centric capability runtime.
2//!
3//! Runtimo provides structured execution, resource limits, crash recovery,
4//! and two-layer telemetry (hardware + process tracking) for machines that
5//! cannot be factory-reset. Every capability execution captures before/after
6//! snapshots, with full audit trails and undo support.
7//!
8//! # Architecture
9//!
10//! - **Capabilities** — Pluggable operations implementing the [`Capability`] trait
11//! - **Jobs** — Lifecycle-tracked execution units (Job, [`JobState`])
12//! - **Telemetry** — Hardware awareness ([`Telemetry`])
13//! - **Process Snapshot** — Running process awareness ([`ProcessSnapshot`])
14//! - **WAL** — Append-only crash recovery log ([`WalWriter`]/[`WalReader`])
15//! - **Backup** — Undo support via pre-mutation file backups ([`BackupManager`])
16//! - **Resource Guards** — Circuit breaker via [`LlmoSafeGuard`]
17//!
18//! # Quick Start
19//!
20//! ```rust
21//! use runtimo_core::{FileRead, Capability, Context};
22//! use serde_json::json;
23//!
24//! let cap = FileRead;
25//! assert_eq!(cap.name(), "FileRead");
26//! ```
27//!
28//! # Execution with Full Telemetry
29//!
30//! ```rust,ignore
31//! use runtimo_core::{FileRead, execute_with_telemetry};
32//! use serde_json::json;
33//! use std::path::Path;
34//!
35//! let cap = FileRead;
36//! let result = execute_with_telemetry(
37//! &cap,
38//! &json!({"path": "/tmp/test.txt"}),
39//! false,
40//! Path::new("/tmp/runtimo.wal"),
41//! ).unwrap();
42//! assert!(result.success);
43//! ```
44//!
45//! # Performance (Measured on AMD EPYC 7B13)
46//!
47//! | Operation | Latency | Notes |
48//! |-----------|---------|-------|
49//! | Cold start | <1s | Binary load + init |
50//! | FileRead | <10ms | Small files (<1KB) |
51//! | FileWrite | <50ms | Includes backup copy |
52//! | Telemetry capture | <100ms | 15+ shell subprocesses |
53//! | Process snapshot | <50ms | ps aux parse |
54//! | Memory baseline | <50MB | RSS at idle |
55//!
56//! # Feature Flags
57//!
58//! No optional features currently. All functionality is included by default.
59
60// Allow idiomatic test lints in test mode as panic/unwrap/indexing are standard in tests.
61#![cfg_attr(
62 test,
63 allow(
64 clippy::unwrap_used,
65 clippy::expect_used,
66 clippy::indexing_slicing,
67 clippy::unused_result_ok
68 )
69)]
70
71pub mod backup;
72/// Pluggable capability implementations (file I/O, shell, git, etc.).
73pub mod capabilities;
74/// Core trait and registry for pluggable operations.
75pub mod capability;
76/// Shell command execution helper.
77pub mod cmd;
78/// Global configuration and path resolution.
79pub mod config;
80/// Capability executor with telemetry and safety guards.
81pub mod executor;
82/// Job identity, state machine, and WAL event types.
83pub mod job;
84/// LLM safety guard — CPU/RAM circuit breakers and entropy source.
85pub mod llmosafe;
86/// Health monitoring with alerting.
87pub mod monitor;
88/// Process snapshot, zombie detection, and top-N queries.
89pub mod processes;
90/// Session tracking for reliable SSH.
91pub mod session;
92/// System telemetry capture and reporting.
93pub mod telemetry;
94/// Path validation against allowed-prefix lists.
95pub mod validation;
96/// Write-ahead log for crash recovery.
97pub mod wal;
98
99pub use backup::BackupManager;
100pub use capabilities::{FileRead, FileWrite, GitExec, Kill, ShellExec, Undo};
101pub use capability::{Capability, CapabilityRegistry, Context, Output};
102pub use config::RuntimoConfig;
103pub use executor::{execute_with_telemetry, execute_with_telemetry_and_session};
104pub use job::{Job, JobId, JobState};
105pub use llmosafe::LlmoSafeGuard;
106pub use monitor::HealthMonitor;
107pub use processes::ProcessSnapshot;
108pub use telemetry::Telemetry;
109pub use wal::{WalEvent, WalEventType, WalReader, WalWriter};
110
111/// Error types for runtimo-core.
112///
113/// Covers all failure modes: state transitions, schema validation,
114/// capability execution, WAL/backup errors, resource limits, and telemetry.
115#[allow(clippy::exhaustive_enums)] // new variants are semver-breaking regardless
116#[derive(Debug, thiserror::Error)]
117pub enum Error {
118 /// Invalid job state transition attempted.
119 #[error("Invalid job state transition: {from:?} -> {to:?}")]
120 InvalidTransition { from: JobState, to: JobState },
121
122 /// JSON schema validation failed for capability arguments.
123 #[error("Schema validation failed: {0}")]
124 SchemaValidationFailed(String),
125
126 /// Requested capability not found in registry.
127 #[error("Capability not found: {0}")]
128 CapabilityNotFound(String),
129
130 /// Capability execution failed.
131 #[error("Execution failed: {0}")]
132 ExecutionFailed(String),
133
134 /// Write-Ahead Log operation failed.
135 #[error("WAL error: {0}")]
136 WalError(String),
137
138 /// Backup/restore operation failed.
139 #[error("Backup error: {0}")]
140 BackupError(String),
141
142 /// Session operation failed (create, load, save, list).
143 #[error("Session error: {0}")]
144 SessionError(String),
145
146 /// System resource limit exceeded (CPU, RAM, or zombie count).
147 #[error("Resource limit exceeded: {0}")]
148 ResourceLimitExceeded(String),
149
150 /// Telemetry capture failed.
151 #[error("Telemetry error: {0}")]
152 TelemetryError(String),
153
154 /// Cognitive safety violation detected by LLMOSafe.
155 #[error("Cognitive safety violation: {0}")]
156 CognitiveSafetyViolation(String),
157}
158
159/// Result alias for runtimo-core operations.
160pub type Result<T> = std::result::Result<T, Error>;
161
162/// Utility functions for path management.
163pub mod utils {
164 use std::path::PathBuf;
165
166 /// Returns the data directory following XDG spec.
167 ///
168 /// Uses `XDG_DATA_HOME` if set, otherwise `~/.local/share/runtimo`.
169 ///
170 /// Falls back to `/tmp/runtimo` with a stderr warning when neither
171 /// `XDG_DATA_HOME` nor `HOME` is set. Data in `/tmp` is not persistent
172 /// across reboots — WAL and backup durability guarantees are degraded
173 /// in this fallback mode.
174 pub fn data_dir() -> PathBuf {
175 let base = std::env::var("XDG_DATA_HOME")
176 .ok()
177 .map(PathBuf::from)
178 .or_else(|| {
179 std::env::var("HOME")
180 .ok()
181 .map(|h| PathBuf::from(h).join(".local/share"))
182 });
183 if let Some(dir) = base {
184 dir.join("runtimo")
185 } else {
186 eprintln!(
187 "[runtimo] Warning: XDG_DATA_HOME and HOME unset — using /tmp/runtimo \
188 (data will not survive reboot)"
189 );
190 PathBuf::from("/tmp/runtimo")
191 }
192 }
193
194 /// Returns the WAL path (env override or default).
195 pub fn wal_path() -> PathBuf {
196 std::env::var("RUNTIMO_WAL_PATH")
197 .map_or_else(|_| data_dir().join("wal.jsonl"), PathBuf::from)
198 }
199
200 /// Returns the backup directory (env override or default).
201 pub fn backup_dir() -> PathBuf {
202 std::env::var("RUNTIMO_BACKUP_DIR")
203 .map_or_else(|_| data_dir().join("backups"), PathBuf::from)
204 }
205
206 /// Generates a unique ID from 16 random bytes (32 hex chars).
207 ///
208 /// Uses `/dev/urandom` for collision resistance — P(collision) < 10⁻¹⁵
209 /// even at 100 IDs/sec for 1 hour. Falls back to timestamp if urandom
210 /// is unavailable (e.g., non-Linux platforms).
211 #[must_use]
212 pub fn generate_id() -> String {
213 let mut bytes = [0u8; 16];
214 if std::fs::File::open("/dev/urandom")
215 .ok()
216 .and_then(|mut f| std::io::Read::read_exact(&mut f, &mut bytes).ok())
217 .is_some()
218 {
219 #[allow(clippy::format_collect)]
220 bytes.iter().map(|b| format!("{b:02x}")).collect()
221 } else {
222 // Fallback: timestamp-based (collision possible but rare)
223 let ts = std::time::SystemTime::now()
224 .duration_since(std::time::UNIX_EPOCH)
225 .unwrap_or_default()
226 .as_nanos();
227 format!("{:x}", ts)
228 }
229 }
230}