muthr 0.1.5

Zero-trust orchestration for autonomous AI agents
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use std::fs;
use std::io::IsTerminal;
use std::os::fd::{FromRawFd, IntoRawFd};
use std::os::unix::process::CommandExt;
use std::path::PathBuf;
use std::process::{Command, Stdio};

use crate::model;
use crate::preset;
use crate::ui;

pub async fn verify_health(port: u16) -> bool {
    model::verify_health("127.0.0.1", port).await
}

pub fn serve(
    profile: Option<String>,
    port: u16,
    foreground: bool,
) -> Result<(), color_eyre::Report> {
    let target_profile = match profile {
        Some(p) => p,
        None => {
            let presets = preset::list_presets()?;
            if presets.is_empty() {
                eprintln!("[ERR] No presets found in ~/.config/muthr/llama/presets/");
                return Ok(());
            }

            let names: Vec<&str> = presets.iter().map(|p| p.name.as_str()).collect();
            match ui::select_list(&names) {
                Some(idx) => presets[idx].name.clone(),
                None => {
                    eprintln!("[INFO] Cancelled.");
                    return Ok(());
                }
            }
        }
    };

    let preset_path = preset::resolve_preset(&target_profile)
        .ok_or_else(|| color_eyre::eyre::eyre!("Preset not found: {}", target_profile))?;

    let opencode_config_path = preset::resolve_opencode_config(&target_profile);
    if let Some(path) = &opencode_config_path {
        println!("[ OK ] Config found: {:?}", path);
    } else {
        eprintln!(
            "[WARN] No matching opencode config at ~/.config/opencode/opencode-{}.json",
            target_profile
        );
    }

    apply_vram_limits();

    let home = std::env::var("HOME")?;
    let cache_dir = PathBuf::from(&home).join(".cache/muthr");
    fs::create_dir_all(&cache_dir)?;

    let tmp_preset = cache_dir.join("active-preset.ini");
    let raw_content = fs::read_to_string(&preset_path)?;
    let expanded = raw_content.replace("~", &home);
    fs::write(&tmp_preset, expanded)?;

    let preset = preset::parse_preset(&preset_path)?;
    let bind_host = preset.global.host.unwrap_or_else(|| "0.0.0.0".to_string());
    let server_port = preset.global.port.unwrap_or(port as u32) as u16;
    let use_jinja = preset.slots.first().is_some_and(|s| s.jinja == Some(true));

    let log_stdout = cache_dir.join("llama-server.log");
    let log_stderr = cache_dir.join("llama-server-err.log");
    let pid_file = cache_dir.join("llama-server.pid");

    if !foreground && pid_file.exists() {
        if let Ok(pid_bytes) = fs::read_to_string(&pid_file) {
            if let Ok(old_pid) = pid_bytes.trim().parse::<u32>() {
                let test = Command::new("kill")
                    .args(["-0", &old_pid.to_string()])
                    .output();
                if let Ok(out) = test {
                    if out.status.success() {
                        eprintln!(
                            "[WARN] Server already running (PID {}). Stopping first.",
                            old_pid
                        );
                        let _ = stop();
                        fs::remove_file(&pid_file).ok();
                        tokio::runtime::Runtime::new().unwrap().block_on(async {
                            tokio::time::sleep(std::time::Duration::from_millis(500)).await;
                        });
                    }
                }
            }
        }
    }

    if foreground {
        println!("[PROC] Starting llama.cpp Server via muthr engine...");
        println!("   Binding Address : http://{}:{}", bind_host, server_port);
        println!("   Profile Target  : {:?}", preset_path);
        println!("   Press Ctrl+C to stop the server.");
        println!();

        let preset_str = tmp_preset.to_string_lossy();
        let port_str = server_port.to_string();
        let mut args: Vec<&str> = vec![
            "--models-preset",
            &preset_str,
            "--port",
            &port_str,
            "--host",
            &bind_host,
            "--prio",
            "2",
        ];
        if use_jinja {
            args.push("--jinja");
        }

        let mut child = Command::new("llama-server")
            .args(&args)
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .spawn()?;

        persist_profile(
            &target_profile,
            preset_path.to_str().unwrap(),
            &opencode_config_path,
        )?;

        let status = child.wait()?;
        if !status.success() {
            eprintln!("[WARN] Server exited with code: {}", status);
        }

        Ok(())
    } else {
        println!("[PROC] Starting llama.cpp Server in background...");
        println!("   Binding Address : http://{}:{}", bind_host, server_port);
        println!("   Profile Target  : {:?}", preset_path);
        println!("   Log file        : {:?}", log_stdout);
        println!("   Error log       : {:?}", log_stderr);
        println!();

        let stdout_file = fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&log_stdout)?;
        let stderr_file = fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&log_stderr)?;

        let stdout_fd = stdout_file.into_raw_fd();
        let stderr_fd = stderr_file.into_raw_fd();

        let preset_str = tmp_preset.to_string_lossy();
        let port_str = server_port.to_string();
        let mut args: Vec<&str> = vec![
            "--models-preset",
            &preset_str,
            "--port",
            &port_str,
            "--host",
            &bind_host,
            "--prio",
            "2",
        ];
        if use_jinja {
            args.push("--jinja");
        }

        let child = unsafe {
            Command::new("llama-server")
                .args(&args)
                .stdout(Stdio::from_raw_fd(stdout_fd))
                .stderr(Stdio::from_raw_fd(stderr_fd))
                .pre_exec(|| {
                    let _ = libc::setsid();
                    Ok(())
                })
                .spawn()?
        };

        let pid = child.id();
        fs::write(&pid_file, pid.to_string())?;
        drop(child);

        persist_profile(
            &target_profile,
            preset_path.to_str().unwrap(),
            &opencode_config_path,
        )?;

        println!("[ OK ] Server started (PID {})", pid);
        println!("   Run 'muthr stop' to stop the server.");

        Ok(())
    }
}

pub fn stop() -> Result<(), color_eyre::Report> {
    let home = std::env::var("HOME")?;
    let cache_dir = PathBuf::from(&home).join(".cache/muthr");
    let pid_file = cache_dir.join("llama-server.pid");

    if !pid_file.exists() {
        println!("[WARN] No PID file found — server may not be running.");
        return Ok(());
    }

    let pid_bytes = fs::read_to_string(&pid_file)?;
    let pid = pid_bytes.trim().parse::<i32>()?;

    let result = Command::new("kill").args(["-9", &pid.to_string()]).output();

    match result {
        Ok(out) if out.status.success() => {
            println!("[ OK ] Server stopped (PID {})", pid);
        }
        Ok(_) => {
            eprintln!(
                "[WARN] Could not kill process {} (may have exited already).",
                pid
            );
        }
        Err(e) => {
            eprintln!("[WARN] Failed to kill process {}: {}", pid, e);
        }
    }

    fs::remove_file(&pid_file).ok();
    Ok(())
}

pub fn status() -> Result<(), color_eyre::Report> {
    let home = std::env::var("HOME")?;
    let profile_path = PathBuf::from(&home).join(".cache/muthr/opencode-profile");

    if !profile_path.exists() {
        println!("[STATUS] No active profile configured.");
        return Ok(());
    }

    let content = fs::read_to_string(&profile_path)?;
    let mut preset_path = String::new();
    let mut config_path = String::new();

    for line in content.lines() {
        if line.starts_with("export LLAMA_ARG_MODELS_PRESET=") {
            preset_path = parse_export_value(line);
        } else if line.starts_with("export OPENCODE_CONFIG=") {
            config_path = parse_export_value(line);
        }
    }

    let preset_basename: String = PathBuf::from(&preset_path)
        .file_stem()
        .and_then(|s| s.to_str())
        .map(String::from)
        .unwrap_or(preset_path.clone());

    println!("[STATUS] Active profile: {}", preset_basename);
    println!("   Preset:     {}", preset_path);
    println!("   OpenCode:   {}", config_path);

    let is_running = Command::new("pgrep")
        .arg("-x")
        .arg("llama-server")
        .output()?
        .status
        .success();

    if is_running {
        println!("   Server:     running");
    } else {
        println!("   Server:     stopped");
    }

    Ok(())
}

pub fn list() -> Result<(), color_eyre::Report> {
    let presets = preset::list_presets()?;
    if presets.is_empty() {
        println!("[WARN] No presets found in ~/.config/muthr/llama/presets/");
        return Ok(());
    }

    let is_tty = std::io::stdout().is_terminal();

    if !is_tty {
        println!("Available presets:");
        println!("===============================================================================");
        for p in &presets {
            let config_status = if preset::resolve_opencode_config(&p.name).is_some() {
                ""
            } else {
                ""
            };
            println!(
                "  {:<30} {} [{}] {}",
                p.name,
                p.path.to_string_lossy(),
                p.slots.len(),
                config_status
            );
        }
        println!("===============================================================================");
        return Ok(());
    }

    let mut rows: Vec<Vec<String>> = Vec::new();
    for p in &presets {
        let config_status = if preset::resolve_opencode_config(&p.name).is_some() {
            "".to_string()
        } else {
            "".to_string()
        };
        rows.push(vec![
            p.name.clone(),
            p.path.to_string_lossy().to_string(),
            p.slots.len().to_string(),
            config_status,
        ]);
    }

    let headers = vec!["Preset", "Path", "Slots", "Config"];

    match ui::select_table(&headers, rows) {
        Some(idx) => {
            println!("{}", presets[idx].name);
        }
        None => {
            println!("Available presets:");
            println!(
                "==============================================================================="
            );
            for p in &presets {
                let config_status = if preset::resolve_opencode_config(&p.name).is_some() {
                    ""
                } else {
                    ""
                };
                println!(
                    "  {:<30} {} [{}] {}",
                    p.name,
                    p.path.to_string_lossy(),
                    p.slots.len(),
                    config_status
                );
            }
            println!(
                "==============================================================================="
            );
        }
    }

    Ok(())
}

fn parse_export_value(line: &str) -> String {
    if let Some(start) = line.find('"') {
        if let Some(end) = line[start + 1..].find('"') {
            return line[start + 1..start + 1 + end].to_string();
        }
    }
    String::new()
}

fn apply_vram_limits() {
    let mem_bytes = sysctl_memsize();
    let threshold: u64 = 32 * 1024 * 1024 * 1024;

    if mem_bytes >= threshold {
        let gb = mem_bytes / 1024 / 1024 / 1024;
        println!(
            "[INFO] High-memory host detected ({}GB). Adjusting wired Metal VRAM limits...",
            gb
        );
        let status = Command::new("sudo")
            .args(["sysctl", "iogpu.wired_limit_mb=43000"])
            .stdin(Stdio::inherit())
            .output();

        match status {
            Ok(out) if out.status.success() => {
                println!("[ OK ] VRAM limits adjusted.");
            }
            Ok(out) => {
                let stderr = String::from_utf8_lossy(&out.stderr);
                eprintln!("[WARN] Failed to adjust VRAM limits: {}", stderr.trim());
            }
            Err(e) => {
                eprintln!("[WARN] Failed to execute sysctl: {}", e);
            }
        }
    } else {
        let gb = mem_bytes / 1024 / 1024 / 1024;
        println!(
            "[INFO] Standard host configuration detected ({}GB). Skipping VRAM limit override.",
            gb
        );
    }
}

fn sysctl_memsize() -> u64 {
    let output = Command::new("sysctl").arg("-n").arg("hw.memsize").output();

    match output {
        Ok(out) if out.status.success() => {
            let raw = String::from_utf8_lossy(&out.stdout);
            let digits: String = raw.chars().filter(|c| c.is_ascii_digit()).collect();
            digits.parse::<u64>().unwrap_or(0)
        }
        _ => 0,
    }
}

fn persist_profile(
    _preset: &str,
    preset_path: &str,
    config_path: &Option<PathBuf>,
) -> Result<(), color_eyre::Report> {
    let home = std::env::var("HOME")?;
    let cache_dir = PathBuf::from(&home).join(".cache/muthr");
    fs::create_dir_all(&cache_dir)?;
    let config_file = cache_dir.join("opencode-profile");

    let config_value = config_path
        .as_ref()
        .map(|p| p.to_string_lossy().to_string())
        .unwrap_or_else(|| "not set".to_string());

    let content = format!(
        "export LLAMA_ARG_MODELS_PRESET=\"{}\"\nexport OPENCODE_CONFIG=\"{}\"",
        preset_path, config_value
    );
    fs::write(&config_file, &content).map_err(|e| {
        color_eyre::eyre::eyre!(
            "Failed to persist profile to {}: {}",
            config_file.display(),
            e
        )
    })?;

    Ok(())
}