Skip to main content

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::{
102    Capability, CapabilityError, CapabilityRegistry, Context, Output, TypedCapability,
103};
104pub use config::RuntimoConfig;
105pub use executor::{execute_with_telemetry, execute_with_telemetry_and_session};
106pub use job::{Job, JobId, JobState};
107pub use llmosafe::LlmoSafeGuard;
108pub use monitor::HealthMonitor;
109pub use processes::ProcessSnapshot;
110pub use telemetry::Telemetry;
111pub use wal::{WalEvent, WalEventType, WalReader, WalWriter};
112
113/// Error types for runtimo-core.
114///
115/// Covers all failure modes: state transitions, schema validation,
116/// capability execution, WAL/backup errors, resource limits, and telemetry.
117#[allow(clippy::exhaustive_enums)] // new variants are semver-breaking regardless
118#[derive(Debug, thiserror::Error)]
119pub enum Error {
120    /// Invalid job state transition attempted.
121    #[error("Invalid job state transition: {from:?} -> {to:?}")]
122    InvalidTransition { from: JobState, to: JobState },
123
124    /// JSON schema validation failed for capability arguments.
125    #[error("Schema validation failed: {0}")]
126    SchemaValidationFailed(String),
127
128    /// Requested capability not found in registry.
129    #[error("Capability not found: {0}")]
130    CapabilityNotFound(String),
131
132    /// Capability execution failed.
133    #[error("Execution failed: {0}")]
134    ExecutionFailed(String),
135
136    /// Write-Ahead Log operation failed.
137    #[error("WAL error: {0}")]
138    WalError(String),
139
140    /// Backup/restore operation failed.
141    #[error("Backup error: {0}")]
142    BackupError(String),
143
144    /// Session operation failed (create, load, save, list).
145    #[error("Session error: {0}")]
146    SessionError(String),
147
148    /// System resource limit exceeded (CPU, RAM, or zombie count).
149    #[error("Resource limit exceeded: {0}")]
150    ResourceLimitExceeded(String),
151
152    /// Telemetry capture failed.
153    #[error("Telemetry error: {0}")]
154    TelemetryError(String),
155
156    /// Cognitive safety violation detected by LLMOSafe.
157    #[error("Cognitive safety violation: {0}")]
158    CognitiveSafetyViolation(String),
159}
160
161/// Result alias for runtimo-core operations.
162pub type Result<T> = std::result::Result<T, Error>;
163
164/// Utility functions for path management.
165pub mod utils {
166    use std::path::PathBuf;
167
168    /// Returns the data directory following XDG spec.
169    ///
170    /// Uses `XDG_DATA_HOME` if set, otherwise `~/.local/share/runtimo`.
171    ///
172    /// Falls back to `/tmp/runtimo` with a stderr warning when neither
173    /// `XDG_DATA_HOME` nor `HOME` is set. Data in `/tmp` is not persistent
174    /// across reboots — WAL and backup durability guarantees are degraded
175    /// in this fallback mode.
176    pub fn data_dir() -> PathBuf {
177        let base = std::env::var("XDG_DATA_HOME")
178            .ok()
179            .map(PathBuf::from)
180            .or_else(|| {
181                std::env::var("HOME")
182                    .ok()
183                    .map(|h| PathBuf::from(h).join(".local/share"))
184            });
185        if let Some(dir) = base {
186            dir.join("runtimo")
187        } else {
188            eprintln!(
189                "[runtimo] Warning: XDG_DATA_HOME and HOME unset — using /tmp/runtimo \
190                 (data will not survive reboot)"
191            );
192            PathBuf::from("/tmp/runtimo")
193        }
194    }
195
196    /// Returns the WAL path (env override or default).
197    pub fn wal_path() -> PathBuf {
198        std::env::var("RUNTIMO_WAL_PATH")
199            .map_or_else(|_| data_dir().join("wal.jsonl"), PathBuf::from)
200    }
201
202    /// Returns the backup directory derived from `data_dir()`.
203    ///
204    /// Always returns `data_dir().join("backups")`. This is a derived path
205    /// from the trusted `data_dir` root — no env var override is available
206    /// (see ADR-C28). External config of the backup location would create
207    /// an attacker control vector.
208    #[must_use]
209    pub fn backup_dir() -> PathBuf {
210        data_dir().join("backups")
211    }
212
213    /// Generates a unique ID from 16 random bytes (32 hex chars).
214    ///
215    /// Uses `/dev/urandom` for collision resistance — P(collision) < 10⁻¹⁵
216    /// even at 100 IDs/sec for 1 hour. Falls back to timestamp if urandom
217    /// is unavailable (e.g., non-Linux platforms).
218    #[must_use]
219    pub fn generate_id() -> String {
220        let mut bytes = [0u8; 16];
221        if std::fs::File::open("/dev/urandom")
222            .ok()
223            .and_then(|mut f| std::io::Read::read_exact(&mut f, &mut bytes).ok())
224            .is_some()
225        {
226            #[allow(clippy::format_collect)]
227            bytes.iter().map(|b| format!("{b:02x}")).collect()
228        } else {
229            // Fallback: timestamp-based (collision possible but rare)
230            let ts = std::time::SystemTime::now()
231                .duration_since(std::time::UNIX_EPOCH)
232                .unwrap_or_default()
233                .as_nanos();
234            format!("{:x}", ts)
235        }
236    }
237}