1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//! Core runtime for AI agent execution with integrated safety and cost controls.
//!
//! Provides agent lifecycle management and local LLM proxy for request interception.
//! Orchestrates all Iron Runtime subsystems (budget, PII detection, analytics, circuit breakers).
//!
//! # Purpose
//!
//! This crate is the execution engine for Iron Runtime:
//! - Agent lifecycle management (spawn, monitor, stop agents)
//! - LLM Router: Local proxy intercepting OpenAI/Anthropic API calls
//! - Integrated safety controls (PII detection, budget enforcement)
//! - Real-time metrics and state management
//! - Dashboard integration via REST API and WebSocket
//!
//! # Architecture
//!
//! Iron Runtime uses a modular architecture with clear separation:
//!
//! ## Core Components
//!
//! 1. **Agent Runtime**: Manages agent processes and lifecycle
//! 2. **LLM Router**: Transparent proxy for LLM API requests
//! 3. **State Manager**: Persists agent state and metrics
//! 4. **Telemetry**: Structured logging for all operations
//!
//! ## Integration Layer
//!
//! Runtime coordinates between modules:
//! - **iron_cost**: Budget validation before LLM requests
//! - **iron_safety**: PII scanning on LLM responses
//! - **iron_runtime_analytics**: Event tracking for dashboard
//! - **iron_reliability**: Circuit breakers for provider failures
//! - **iron_runtime_state**: Agent state persistence
//!
//! ## Python Bindings
//!
//! Python bindings are provided by the `iron_sdk` crate (see ADR-010).
//! This crate (`iron_runtime`) is pure Rust with no PyO3 dependencies.
//!
//! # Key Types
//!
//! - [`AgentRuntime`] - Main runtime managing agent lifecycle
//! - [`RuntimeConfig`] - Runtime configuration (budget, verbosity)
//! - [`AgentHandle`] - Handle to running agent for control
//! - [`llm_router::LlmRouter`] - Local LLM proxy server
//!
//! # Public API
//!
//! ## Rust API
//!
//! ```rust,no_run
//! use iron_runtime::{AgentRuntime, RuntimeConfig};
//! use std::path::Path;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), anyhow::Error> {
//! // Configure runtime
//! let config = RuntimeConfig {
//! budget: 100.0, // $100 budget
//! verbose: true,
//! };
//!
//! // Create runtime
//! let runtime = AgentRuntime::new(config);
//!
//! // Start agent from Python script
//! let handle = runtime.start_agent(Path::new("agent.py")).await?;
//! println!("Agent started: {}", handle.agent_id.as_str());
//!
//! // Monitor metrics
//! if let Some(metrics) = runtime.get_metrics(handle.agent_id.as_str()) {
//! println!("Budget spent: ${}", metrics.budget_spent);
//! println!("PII detections: {}", metrics.pii_detections);
//! }
//!
//! // Stop agent
//! runtime.stop_agent(handle.agent_id.as_str()).await?;
//! Ok(())
//! }
//! ```
//!
//! # Safety Controls
//!
//! Runtime enforces multiple safety layers:
//!
//! ## Budget Enforcement
//!
//! - Pre-request budget validation
//! - Request blocked if budget exceeded
//! - Real-time cost tracking
//! - Budget alerts at configurable thresholds
//!
//! ## PII Detection
//!
//! - Scans all LLM responses for PII
//! - Automatic redaction of sensitive data
//! - Compliance audit logging
//! - Configurable detection patterns
//!
//! ## Circuit Breakers
//!
//! - Detects failing LLM providers
//! - Fast-fail on known-bad endpoints
//! - Automatic recovery after timeout
//! - Per-provider state isolation
//!
//! # Feature Flags
//!
//! - `enabled` - Enable full runtime (disabled for library-only builds)
//! - `analytics` - Enable analytics recording via iron_runtime_analytics
//!
//! # Performance
//!
//! Runtime overhead on LLM requests:
//! - Budget check: <1ms
//! - PII detection: <5ms per KB
//! - Circuit breaker check: <0.1ms
//! - Analytics recording: <0.5ms
//! - Total proxy overhead: <10ms per request
//!
//! Streaming responses have near-zero buffering latency.
// LLM Router module
pub use *;
pub use *;