aprender-orchestrate 0.30.0

Sovereign AI orchestration: autonomous agents, ML serving, code analysis, and transpilation pipelines
Documentation
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Agent CLI helper functions (build, validate, format).

use crate::ansi_colors::Colorize;
use std::path::PathBuf;
use std::sync::Arc;

/// Auto-pull model if `model_repo` is set and file is missing.
pub(super) fn try_auto_pull(manifest: &batuta::agent::AgentManifest) -> anyhow::Result<()> {
    if let Some(repo) = manifest.model.needs_pull() {
        println!("{} Auto-pulling model: {}", "".bright_cyan(), repo.cyan());
        let quant = manifest.model.model_quantization.as_deref().unwrap_or("q4_k_m");
        println!("  Quantization: {}, Timeout: 600s", quant);
        match manifest.model.auto_pull(600) {
            Ok(path) => {
                println!("{} Model downloaded: {}", "".green(), path.display(),);
            }
            Err(e) => {
                anyhow::bail!("auto-pull failed: {e}");
            }
        }
    }
    Ok(())
}

/// Build the LLM driver from manifest configuration.
///
/// Supports three modes:
/// - Local only: RealizarDriver (model_path + inference feature)
/// - Remote only: RemoteDriver (remote_model + native feature)
/// - Hybrid: RoutingDriver (local-first, remote fallback)
/// - Fallback: MockDriver (dry-run when nothing configured)
pub(super) fn build_driver(
    manifest: &batuta::agent::AgentManifest,
) -> anyhow::Result<Box<dyn batuta::agent::driver::LlmDriver>> {
    let resolved_path = manifest.model.resolve_model_path();
    let local = build_local_driver(manifest, &resolved_path);
    let remote = build_remote_driver(manifest);

    match (local, remote) {
        (Some(primary), Some(fallback)) => {
            println!("{} Routing: local-first with remote fallback", "".bright_cyan());
            Ok(Box::new(batuta::agent::driver::router::RoutingDriver::new(primary, fallback)))
        }
        (Some(driver), None) => Ok(driver),
        (None, Some(driver)) => Ok(driver),
        (None, None) => {
            if resolved_path.is_some() {
                #[cfg(not(feature = "inference"))]
                println!(
                    "{} inference feature not enabled; rebuild with: {}",
                    "".bright_yellow(),
                    "cargo build --features inference".cyan()
                );
            } else {
                println!(
                    "{} No model configured; set model_path or remote_model",
                    "".bright_blue()
                );
            }
            Ok(Box::new(batuta::agent::driver::mock::MockDriver::single_response(
                "Hello! I'm running in dry-run mode. \
                     Set model_path or remote_model in your agent manifest.",
            )))
        }
    }
}

/// Build local inference driver (RealizarDriver) if available.
fn build_local_driver(
    manifest: &batuta::agent::AgentManifest,
    resolved_path: &Option<std::path::PathBuf>,
) -> Option<Box<dyn batuta::agent::driver::LlmDriver>> {
    #[cfg(feature = "inference")]
    if let Some(ref model_path) = resolved_path {
        match batuta::agent::driver::realizar::RealizarDriver::new(
            model_path.clone(),
            manifest.model.context_window,
        ) {
            Ok(d) => return Some(Box::new(d)),
            Err(e) => {
                eprintln!("Warning: local driver init failed: {e}");
            }
        }
    }
    let _ = (manifest, resolved_path);
    None
}

/// Build remote API driver if remote_model is configured.
fn build_remote_driver(
    manifest: &batuta::agent::AgentManifest,
) -> Option<Box<dyn batuta::agent::driver::LlmDriver>> {
    #[cfg(feature = "native")]
    if let Some(ref model_id) = manifest.model.remote_model {
        use batuta::agent::driver::remote::{ApiProvider, RemoteDriver, RemoteDriverConfig};
        let (provider, base_url, env_key) = if model_id.starts_with("claude") {
            (ApiProvider::Anthropic, "https://api.anthropic.com", "ANTHROPIC_API_KEY")
        } else {
            (ApiProvider::OpenAi, "https://api.openai.com", "OPENAI_API_KEY")
        };
        let api_key = match std::env::var(env_key) {
            Ok(k) if !k.is_empty() => k,
            _ => {
                println!(
                    "{} Remote model set but {} not found; skipping remote driver",
                    "".bright_yellow(),
                    env_key
                );
                return None;
            }
        };
        let config = RemoteDriverConfig {
            base_url: base_url.into(),
            api_key,
            model: model_id.clone(),
            provider,
            context_window: manifest.model.context_window.unwrap_or(8192),
        };
        return Some(Box::new(RemoteDriver::new(config)));
    }
    let _ = manifest;
    None
}

/// Build tool registry from manifest capabilities.
pub(super) fn build_tool_registry(
    manifest: &batuta::agent::AgentManifest,
) -> batuta::agent::tool::ToolRegistry {
    use batuta::agent::capability::Capability;
    use batuta::agent::tool::ToolRegistry;

    let mut registry = ToolRegistry::new();

    for cap in &manifest.capabilities {
        match cap {
            Capability::Memory => {
                let memory = Arc::new(batuta::agent::memory::InMemorySubstrate::new());
                registry.register(Box::new(batuta::agent::tool::memory::MemoryTool::new(
                    memory,
                    manifest.name.clone(),
                )));
            }
            Capability::Compute => {
                let cwd = std::env::current_dir().unwrap_or_default().to_string_lossy().to_string();
                registry.register(Box::new(batuta::agent::tool::compute::ComputeTool::new(cwd)));
            }
            Capability::Shell { allowed_commands } => {
                let cwd = std::env::current_dir().unwrap_or_default();
                registry.register(Box::new(batuta::agent::tool::shell::ShellTool::new(
                    allowed_commands.clone(),
                    cwd,
                )));
            }
            Capability::Network { allowed_hosts } => {
                registry.register(Box::new(batuta::agent::tool::network::NetworkTool::new(
                    allowed_hosts.clone(),
                )));
            }
            #[cfg(feature = "agents-browser")]
            Capability::Browser => {
                registry.register(Box::new(batuta::agent::tool::browser::BrowserTool::new(
                    manifest.privacy,
                )));
            }
            #[cfg(feature = "rag")]
            Capability::Rag => {
                let oracle = Arc::new(batuta::oracle::rag::RagOracle::new());
                registry.register(Box::new(batuta::agent::tool::rag::RagTool::new(oracle, 5)));
            }
            // Mcp registered via register_mcp_tools (agents-mcp).
            _ => {}
        }
    }

    registry
}

/// Register SpawnTool on the registry if Spawn capability is present.
pub(super) fn register_spawn_tool(
    registry: &mut batuta::agent::tool::ToolRegistry,
    manifest: &batuta::agent::AgentManifest,
    driver: Arc<dyn batuta::agent::driver::LlmDriver>,
) {
    use batuta::agent::capability::Capability;
    for cap in &manifest.capabilities {
        if let Capability::Spawn { max_depth } = cap {
            let pool = Arc::new(tokio::sync::Mutex::new(batuta::agent::pool::AgentPool::new(
                Arc::clone(&driver),
                4,
            )));
            registry.register(Box::new(batuta::agent::tool::spawn::SpawnTool::new(
                pool,
                manifest.clone(),
                0,
                *max_depth,
            )));
            break;
        }
    }
}

/// Register InferenceTool on the registry if Inference capability is present.
pub(super) fn register_inference_tool(
    registry: &mut batuta::agent::tool::ToolRegistry,
    manifest: &batuta::agent::AgentManifest,
    driver: Arc<dyn batuta::agent::driver::LlmDriver>,
) {
    use batuta::agent::capability::Capability;
    if manifest.capabilities.contains(&Capability::Inference) {
        let max_tokens = manifest.model.max_tokens;
        registry.register(Box::new(batuta::agent::tool::inference::InferenceTool::new(
            driver, max_tokens,
        )));
    }
}

/// Register MCP client tools discovered from manifest mcp_servers.
#[cfg(feature = "agents-mcp")]
pub(super) async fn register_mcp_tools(
    registry: &mut batuta::agent::tool::ToolRegistry,
    manifest: &batuta::agent::AgentManifest,
) {
    let tools = batuta::agent::tool::mcp_client::discover_mcp_tools(manifest).await;
    for tool in tools {
        registry.register(Box::new(tool));
    }
}

/// Build memory substrate (TruenoMemory when available, else InMemory).
pub(super) fn build_memory() -> Box<dyn batuta::agent::memory::MemorySubstrate> {
    #[cfg(feature = "rag")]
    {
        match batuta::agent::memory::TruenoMemory::open_in_memory() {
            Ok(mem) => return Box::new(mem),
            Err(e) => {
                eprintln!(
                    "Warning: TruenoMemory init failed ({e}), \
                     falling back to InMemorySubstrate"
                );
            }
        }
    }
    Box::new(batuta::agent::memory::InMemorySubstrate::new())
}

/// Print a stream event to stdout.
pub(super) fn print_stream_event(event: &batuta::agent::driver::StreamEvent) {
    use batuta::agent::driver::StreamEvent;
    match event {
        StreamEvent::PhaseChange { phase } => println!("  {} Phase: {phase:?}", "".bright_blue()),
        StreamEvent::ToolUseStart { name, .. } => {
            println!("  {} Tool: {}", "".bright_yellow(), name.cyan());
        }
        StreamEvent::ToolUseEnd { name, result, .. } => {
            let p =
                if result.len() > 80 { format!("{}...", &result[..77]) } else { result.clone() };
            println!("  {} {}{}", "".green(), name, p.dimmed());
        }
        StreamEvent::TextDelta { text } => print!("{text}"),
        StreamEvent::ContentComplete { .. } => {}
    }
}

/// Validate model file integrity (G0) and format (G1).
pub(super) fn validate_model_file(manifest: &batuta::agent::AgentManifest) -> anyhow::Result<()> {
    let model_path = manifest
        .model
        .resolve_model_path()
        .ok_or_else(|| anyhow::anyhow!("no model_path or model_repo configured"))?;

    println!();
    println!("{} Model Validation (G0-G1)", "🔍".bright_cyan().bold());
    println!("{}", "".repeat(40).dimmed());

    if !model_path.exists() {
        println!("  {} G0 FAIL: model file not found: {}", "".bright_red(), model_path.display());
        anyhow::bail!("G0: model file not found: {}", model_path.display());
    }

    let metadata = std::fs::metadata(&model_path)
        .map_err(|e| anyhow::anyhow!("cannot stat {}: {e}", model_path.display()))?;
    let size_mb = metadata.len() as f64 / (1024.0 * 1024.0);
    let file_bytes = std::fs::read(&model_path)
        .map_err(|e| anyhow::anyhow!("cannot read {}: {e}", model_path.display()))?;
    let hash_hex = blake3::hash(&file_bytes).to_hex();

    println!("  {} G0 PASS: {} ({:.1} MB)", "".green(), model_path.display(), size_mb);
    println!("    BLAKE3: {}", &hash_hex[..32]);

    let format = detect_model_format(&file_bytes);
    println!("  {} G1: Format detected: {}", "".green(), format.bright_yellow());
    Ok(())
}

/// Validate model inference sanity (G2 gate).
pub(super) fn validate_model_g2(manifest: &batuta::agent::AgentManifest) -> anyhow::Result<()> {
    println!();
    println!("{} G2: Inference Sanity Check", "🧪".bright_cyan().bold());
    println!("{}", "".repeat(40).dimmed());

    let driver = build_driver(manifest)?;

    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .map_err(|e| anyhow::anyhow!("tokio runtime: {e}"))?;

    let probe_prompt = "Respond with exactly: Hello, I am operational.";

    let request = batuta::agent::driver::CompletionRequest {
        model: String::new(),
        messages: vec![batuta::agent::driver::Message::User(probe_prompt.into())],
        max_tokens: 64,
        temperature: 0.0,
        tools: vec![],
        system: Some(manifest.model.system_prompt.clone()),
    };

    let result = rt.block_on(async { driver.complete(request).await });

    match result {
        Ok(response) => {
            let text = &response.text;
            if text.is_empty() {
                println!("  {} G2 FAIL: empty response", "".bright_red());
                anyhow::bail!("G2: model returned empty response");
            }
            let entropy = char_entropy(text);
            let dot = "".bright_blue();
            println!("  {dot} G2 probe: \"{}\"", truncate_str(text, 60));
            println!("  {dot} G2 metrics: len={}, entropy={:.2}", text.len(), entropy);
            if entropy > 5.5 {
                println!(
                    "  {} G2 WARN: high entropy ({:.2}) — check LAYOUT-002",
                    "".bright_yellow(),
                    entropy
                );
            }
            println!("  {} G2 PASS: model produces coherent output", "".green());
            Ok(())
        }
        Err(e) => {
            println!("  {} G2 FAIL: inference error: {e}", "".bright_red());
            anyhow::bail!("G2: inference failed: {e}");
        }
    }
}

/// Shannon entropy of a string (bits per character).
pub(super) fn char_entropy(s: &str) -> f64 {
    if s.is_empty() {
        return 0.0;
    }
    let mut freq = [0u32; 256];
    let total = s.len() as f64;
    for b in s.bytes() {
        freq[b as usize] += 1;
    }
    let mut entropy = 0.0;
    for &count in &freq {
        if count > 0 {
            let p = count as f64 / total;
            entropy -= p * p.log2();
        }
    }
    entropy
}

/// Truncate a string to max_len with ellipsis.
pub(super) fn truncate_str(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        s.to_string()
    } else {
        format!("{}...", &s[..max_len.saturating_sub(3)])
    }
}

/// Detect model format from magic bytes.
pub(super) fn detect_model_format(data: &[u8]) -> &'static str {
    if data.len() >= 4 {
        // GGUF magic: 0x46475547 ("GGUF" in LE)
        if data[..4] == [0x47, 0x47, 0x55, 0x46] {
            return "GGUF";
        }
        // APR v2 magic: "APR\x02"
        if data[..4] == [b'A', b'P', b'R', 0x02] {
            return "APR v2";
        }
        // SafeTensors: starts with JSON length (LE u64) then '{'
        if data.len() >= 9 && data[8] == b'{' {
            return "SafeTensors";
        }
    }
    "unknown"
}

/// Load and parse an agent manifest from TOML.
pub(super) fn load_manifest(path: &PathBuf) -> anyhow::Result<batuta::agent::AgentManifest> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| anyhow::anyhow!("Cannot read manifest {}: {e}", path.display()))?;
    batuta::agent::AgentManifest::from_toml(&content)
        .map_err(|e| anyhow::anyhow!("Invalid manifest {}: {e}", path.display()))
}

/// Print a summary of the loaded manifest.
pub(super) fn print_manifest_summary(manifest: &batuta::agent::AgentManifest) {
    let dot = "".bright_blue();
    println!("{}", "🤖 Batuta Agent Runtime (Sovereign)".bright_cyan().bold());
    println!("{}", "".repeat(60).dimmed());
    println!("{dot} Agent: {}", manifest.name.cyan());
    println!("{dot} Version: {}", manifest.version.dimmed());
    println!("{dot} Privacy: {:?}", manifest.privacy);
    println!("{dot} Capabilities: {:?}", manifest.capabilities);
    println!("{dot} Max iterations: {}", manifest.resources.max_iterations);
    if let Some(ref path) = manifest.model.model_path {
        println!("{dot} Model: {}", path.display());
    } else if let Some(ref repo) = manifest.model.model_repo {
        let q = manifest.model.model_quantization.as_deref().unwrap_or("q4_k_m");
        println!("{dot} Model: {} ({q})", repo.cyan());
    } else {
        println!("{dot} Model: {}", "none (specify model_path or model_repo)".dimmed());
    }
}

/// Build a LoopGuard from manifest + optional override.
pub(super) fn build_guard(
    manifest: &batuta::agent::AgentManifest,
    max_iterations: Option<u32>,
) -> (u32, u32) {
    let max_iter = max_iterations.unwrap_or(manifest.resources.max_iterations);
    let max_tools = manifest.resources.max_tool_calls;
    (max_iter, max_tools)
}