muthr 0.1.47

Zero-trust orchestrator for MLX inference, container-based sandboxes, and MCP services on Apple Silicon
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// Copyright 2026 tappunk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::os::unix::process::CommandExt;
use std::path::PathBuf;
use std::process::Stdio;
use std::time::Duration;

use tokio::fs;
use tokio::process::Command as AsyncCommand;
use tokio::signal::unix::{SignalKind, signal};

use crate::config;
use crate::model;

pub const ENGINE_NAME: &str = "mlxcel";
pub const EXECUTABLE: &str = "mlxcel-server";
pub const PID_FILE_NAME: &str = "mlxcel-server.pid";
pub const ACTIVE_PRESET_FILE: &str = "active-preset-name-mlxcel";
pub const LOG_STDOUT: &str = "mlxcel-server.log";
pub const LOG_RE_ERR: &str = "mlxcel-server-err.log";

const DEFAULT_MODEL_ID: &str = "mlx-community/Qwen3.5-9B-MLX-4bit";
const BIND_HOST: &str = "127.0.0.1";

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

fn is_process_alive(pid: u32) -> bool {
    unsafe { libc::kill(pid as i32, 0) == 0 }
}

fn pid_file_path() -> Option<PathBuf> {
    std::env::var("HOME")
        .ok()
        .map(|h| PathBuf::from(h).join(format!(".cache/muthr/{}", PID_FILE_NAME)))
}

fn kill_runtime_target(pid: u32, sig: i32) {
    let pgid = unsafe { libc::getpgid(pid as i32) };
    if pgid > 0 {
        unsafe {
            libc::kill(-pgid, sig);
        }
    } else {
        unsafe {
            libc::kill(pid as i32, sig);
        }
    }
}

fn matches_runtime_process(comm: &str, args: &str) -> bool {
    comm == EXECUTABLE || args.contains(" mlxcel-server") || args.contains("/mlxcel-server")
}

async fn is_runtime_pid(pid: u32) -> bool {
    if !is_process_alive(pid) {
        return false;
    }

    let output = AsyncCommand::new("ps")
        .args(["-p", &pid.to_string(), "-o", "comm=", "-o", "args="])
        .output()
        .await;

    if let Ok(out) = output {
        let ps_output = String::from_utf8_lossy(&out.stdout);
        for line in ps_output.lines() {
            let trimmed = line.trim();
            let parts: Vec<&str> = trimmed.splitn(2, ' ').collect();
            if parts.len() < 2 {
                continue;
            }
            let comm = parts[0].trim();
            let args = parts[1];
            if matches_runtime_process(comm, args) {
                return true;
            }
        }
    }

    false
}

async fn list_runtime_pids() -> Vec<u32> {
    let output = AsyncCommand::new("ps")
        .args(["-axo", "pid=,comm=,args="])
        .output()
        .await;
    let mut pids = Vec::new();

    if let Ok(out) = output {
        let stdout = String::from_utf8_lossy(&out.stdout);
        for line in stdout.lines() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }

            let mut parts = trimmed.split_whitespace();
            let pid_str = match parts.next() {
                Some(v) => v,
                None => continue,
            };
            let comm = match parts.next() {
                Some(v) => v,
                None => continue,
            };
            let args = parts.collect::<Vec<_>>().join(" ");

            if let Ok(pid) = pid_str.parse::<u32>()
                && matches_runtime_process(comm, &args)
            {
                pids.push(pid);
            }
        }
    }

    pids.sort_unstable();
    pids.dedup();
    pids
}

async fn list_container_sandboxes() -> Vec<(String, String)> {
    let output = AsyncCommand::new("container")
        .args(["list", "--all", "--format", "json"])
        .output()
        .await;
    let Ok(out) = output else {
        return Vec::new();
    };
    if !out.status.success() {
        return Vec::new();
    }

    let Ok(items) = serde_json::from_slice::<Vec<serde_json::Value>>(&out.stdout) else {
        return Vec::new();
    };

    let mut rows = Vec::new();
    for item in items {
        let id = item
            .get("id")
            .and_then(|v| v.as_str())
            .or_else(|| {
                item.get("configuration")
                    .and_then(|v| v.get("id"))
                    .and_then(|v| v.as_str())
            })
            .unwrap_or_default()
            .to_string();
        if !id.starts_with("muthr-") || id == "muthr-services" || id == "muthr-searxng" {
            continue;
        }

        let status = item
            .get("status")
            .and_then(|v| v.as_str())
            .or_else(|| item.get("state").and_then(|v| v.as_str()))
            .or_else(|| {
                item.get("status")
                    .and_then(|v| v.get("state"))
                    .and_then(|v| v.as_str())
            })
            .unwrap_or("unknown")
            .to_string();

        let token = id.strip_prefix("muthr-").unwrap_or(&id).to_string();
        rows.push((token, status));
    }

    rows.sort_by(|a, b| a.0.cmp(&b.0));
    rows
}

fn resolve_model_id(profile: Option<String>) -> String {
    profile
        .map(|p| p.trim().to_string())
        .filter(|p| !p.is_empty())
        .or_else(|| {
            config::load()
                .ok()
                .and_then(|cfg| cfg.default_engine_profile)
                .map(|p| p.trim().to_string())
                .filter(|p| !p.is_empty())
        })
        .unwrap_or_else(|| DEFAULT_MODEL_ID.to_string())
}

fn build_command(model_id: &str, host: &str, port: u16) -> std::process::Command {
    let mut cmd = std::process::Command::new(EXECUTABLE);
    cmd.arg("--model").arg(model_id);
    cmd.arg("--host").arg(host);
    cmd.arg("--port").arg(port.to_string());
    cmd
}

pub async fn is_running() -> bool {
    let pid_file = match pid_file_path() {
        Some(path) => path,
        None => return !list_runtime_pids().await.is_empty(),
    };

    if pid_file.exists() {
        let pid_bytes = match fs::read_to_string(&pid_file).await {
            Ok(b) => b,
            Err(_) => return !list_runtime_pids().await.is_empty(),
        };

        let pid = match pid_bytes.trim().parse::<u32>() {
            Ok(p) => p,
            Err(_) => {
                fs::remove_file(&pid_file).await.ok();
                return !list_runtime_pids().await.is_empty();
            }
        };

        if is_runtime_pid(pid).await {
            return true;
        }

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

    !list_runtime_pids().await.is_empty()
}

pub async fn start(
    profile: Option<String>,
    port: u16,
    foreground: bool,
) -> Result<(), color_eyre::Report> {
    let _engine_lock = crate::lifecycle::acquire("engine", Duration::from_secs(20)).await?;
    let model_id = resolve_model_id(profile);

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

    let log_stdout = cache_dir.join(LOG_STDOUT);
    let log_stderr = cache_dir.join(LOG_RE_ERR);
    let pid_file = cache_dir.join(PID_FILE_NAME);

    let existing_pids = list_runtime_pids().await;
    if !existing_pids.is_empty() {
        eprintln!(
            "warning: found running {} process(es), stopping before start",
            ENGINE_NAME
        );
        for pid in existing_pids {
            stop_pid(pid).await;
        }
        tokio::time::sleep(std::time::Duration::from_millis(300)).await;
    }
    fs::remove_file(&pid_file).await.ok();

    if foreground {
        eprintln!("info: {} starting on {}:{}", ENGINE_NAME, BIND_HOST, port);

        let mut child = AsyncCommand::new(EXECUTABLE);
        child
            .arg("--model")
            .arg(&model_id)
            .arg("--host")
            .arg(BIND_HOST)
            .arg("--port")
            .arg(port.to_string())
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit());

        let mut child = child.spawn()?;
        let child_pid = child.id().unwrap_or_default();

        let active_model_path = cache_dir.join(ACTIVE_PRESET_FILE);
        fs::write(&active_model_path, &model_id).await?;

        let mut sigterm = signal(SignalKind::terminate())?;
        let mut sigint = signal(SignalKind::interrupt())?;

        loop {
            tokio::select! {
                maybe_status = child.wait() => {
                    let status = maybe_status?;
                    if !status.success() {
                        eprintln!("error: server exited with code {}", status);
                    }
                    return Ok(());
                }
                _ = sigterm.recv() => {
                    if child_pid != 0 {
                        eprintln!("info: forwarding SIGTERM to {} pid {}", ENGINE_NAME, child_pid);
                        kill_runtime_target(child_pid, libc::SIGTERM);
                    }
                }
                _ = sigint.recv() => {
                    if child_pid != 0 {
                        eprintln!("info: forwarding SIGINT to {} pid {}", ENGINE_NAME, child_pid);
                        kill_runtime_target(child_pid, libc::SIGTERM);
                    }
                }
            }
        }
    }

    eprintln!(
        "{} starting (background) on {}:{}",
        ENGINE_NAME, BIND_HOST, port
    );

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

    let mut cmd = build_command(&model_id, BIND_HOST, port);
    cmd.stdout(stdout_file).stderr(stderr_file);

    unsafe {
        cmd.pre_exec(|| {
            if libc::setsid() == -1 {
                return Err(std::io::Error::last_os_error());
            }
            Ok(())
        });
    }

    match cmd.spawn() {
        Ok(c) => {
            let pid = c.id();
            fs::write(&pid_file, pid.to_string()).await?;
            let active_model_path = cache_dir.join(ACTIVE_PRESET_FILE);
            fs::write(&active_model_path, &model_id).await?;
            eprintln!("info: started pid {}", pid);
            Ok(())
        }
        Err(e) => Err(e.into()),
    }
}

pub async fn stop() -> Result<(), color_eyre::Report> {
    let _engine_lock = crate::lifecycle::acquire("engine", Duration::from_secs(20)).await?;
    let home = std::env::var("HOME")?;
    let cache_dir = PathBuf::from(&home).join(".cache/muthr");
    let pid_file = cache_dir.join(PID_FILE_NAME);

    let mut target_pids = Vec::new();
    if pid_file.exists()
        && let Ok(pid_bytes) = fs::read_to_string(&pid_file).await
        && let Ok(pid) = pid_bytes.trim().parse::<u32>()
    {
        if is_runtime_pid(pid).await {
            target_pids.push(pid);
        } else {
            eprintln!(
                "warning: stale pid file for non-{} process {}, removing",
                ENGINE_NAME, pid
            );
        }
    }

    for pid in list_runtime_pids().await {
        if !target_pids.contains(&pid) {
            target_pids.push(pid);
        }
    }

    if target_pids.is_empty() {
        fs::remove_file(&pid_file).await.ok();
        return Ok(());
    }

    for pid in target_pids {
        stop_pid(pid).await;
    }

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

pub async fn stop_all() -> Result<(), color_eyre::Report> {
    stop().await
}

async fn stop_pid(pid: u32) {
    if !is_runtime_pid(pid).await {
        return;
    }

    eprintln!("info: stopping {} pid {}", ENGINE_NAME, pid);
    kill_runtime_target(pid, libc::SIGTERM);

    let mut died = false;
    for _ in 0..30 {
        tokio::time::sleep(std::time::Duration::from_millis(500)).await;
        if !is_process_alive(pid) {
            died = true;
            break;
        }
    }

    if died {
        eprintln!("info: stopped {} pid {}", ENGINE_NAME, pid);
    } else {
        eprintln!(
            "warning: sigterm failed for {} pid {}, escalating to sigkill",
            ENGINE_NAME, pid
        );
        kill_runtime_target(pid, libc::SIGKILL);
        eprintln!("info: killed {} pid {}", ENGINE_NAME, pid);
    }
}

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

    let active_model = fs::read_to_string(cache_dir.join(ACTIVE_PRESET_FILE))
        .await
        .ok()
        .map(|s| s.trim().to_string())
        .unwrap_or_default();

    let running = is_running().await;
    let any_model = !active_model.is_empty();

    let overall_state = if !any_model {
        "not_configured"
    } else if running {
        "running"
    } else {
        "configured_stopped"
    };

    if output == crate::OutputFormat::Json || output == crate::OutputFormat::Ndjson {
        let payload = serde_json::json!({
            "state": overall_state,
            ENGINE_NAME: {
                "model": if active_model.is_empty() { serde_json::Value::Null } else { serde_json::Value::String(active_model.clone()) },
                "server_running": running,
            }
        });
        println!("{}", serde_json::to_string(&payload)?);
        return Ok(());
    }

    if !any_model {
        eprintln!("muthr: not configured");
    } else if running {
        eprintln!("muthr: running");
    } else {
        eprintln!("muthr: configured, stopped");
    }

    print_runtime_status(&active_model, running);

    let services_container = "muthr-services";
    let searxng_container = "muthr-searxng";
    let container_items = AsyncCommand::new("container")
        .args(["list", "--all", "--format", "json"])
        .output()
        .await
        .ok()
        .filter(|out| out.status.success())
        .and_then(|out| serde_json::from_slice::<Vec<serde_json::Value>>(&out.stdout).ok())
        .unwrap_or_default();

    let mut services_status: Option<&str> = None;
    for item in &container_items {
        let id = item
            .get("id")
            .and_then(|v| v.as_str())
            .or_else(|| {
                item.get("configuration")
                    .and_then(|v| v.get("id"))
                    .and_then(|v| v.as_str())
            })
            .unwrap_or_default();
        if id != services_container {
            continue;
        }
        let state = item
            .get("status")
            .and_then(|v| v.get("state"))
            .and_then(|v| v.as_str())
            .or_else(|| item.get("state").and_then(|v| v.as_str()))
            .unwrap_or("unknown");
        services_status = Some(if state.eq_ignore_ascii_case("running") {
            "running"
        } else {
            "stopped"
        });
        break;
    }

    if let Some(status) = services_status {
        eprintln!("  services mcp     {}     {}", services_container, status);

        let provision_output = AsyncCommand::new("container")
            .args([
                "exec",
                services_container,
                "bash",
                "-c",
                "test -f $HOME/mcp-stdio.sh && test -f $HOME/.local/lib/node_modules/mcp-searxng/dist/cli.js",
            ])
            .output()
            .await
            .ok();

        match provision_output {
            Some(out) if out.status.success() => {
                eprintln!("  mcp provisioned  yes");
            }
            _ => {
                eprintln!("  mcp provisioned  no");
            }
        }
    }

    let mut searxng_status: Option<&str> = None;
    for item in &container_items {
        let id = item
            .get("id")
            .and_then(|v| v.as_str())
            .or_else(|| {
                item.get("configuration")
                    .and_then(|v| v.get("id"))
                    .and_then(|v| v.as_str())
            })
            .unwrap_or_default();
        if id != searxng_container {
            continue;
        }
        let state = item
            .get("status")
            .and_then(|v| v.get("state"))
            .and_then(|v| v.as_str())
            .or_else(|| item.get("state").and_then(|v| v.as_str()))
            .unwrap_or("unknown");
        searxng_status = Some(if state.eq_ignore_ascii_case("running") {
            "running"
        } else {
            "stopped"
        });
        break;
    }

    if let Some(status) = searxng_status {
        eprintln!("  services searxng {}     {}", searxng_container, status);
    }

    let mut active_sandboxes: Vec<(String, String)> = Vec::new();

    for row in list_container_sandboxes().await {
        if !active_sandboxes.iter().any(|(t, _)| *t == row.0) {
            active_sandboxes.push(row);
        }
    }

    if !active_sandboxes.is_empty() {
        eprintln!("  sandboxes");
        for (i, (token, status)) in active_sandboxes.iter().enumerate() {
            let connector = if i + 1 == active_sandboxes.len() {
                "    └─"
            } else {
                "    ├─"
            };
            eprintln!("{} {:<20} {}", connector, token, status);
        }
    }

    Ok(())
}

fn print_runtime_status(model_id: &str, is_running: bool) {
    if model_id.is_empty() {
        eprintln!("  engine {:<10} (none)", ENGINE_NAME);
    } else {
        eprintln!("  engine {:<10} active      {}", ENGINE_NAME, model_id);
    }

    if is_running {
        eprintln!("  server {:<10} running", ENGINE_NAME);
    } else {
        eprintln!("  server {:<10} stopped", ENGINE_NAME);
    }
}

pub fn presets(output: crate::OutputFormat) -> Result<(), color_eyre::Report> {
    let default_model = config::load()?
        .default_engine_profile
        .unwrap_or_else(|| DEFAULT_MODEL_ID.to_string());

    if output == crate::OutputFormat::Json {
        let payload = vec![serde_json::json!({
            "id": default_model,
            "runtime": ENGINE_NAME,
        })];
        println!("{}", serde_json::to_string(&payload)?);
        return Ok(());
    }

    if output == crate::OutputFormat::Ndjson {
        let payload = serde_json::json!({
            "id": default_model,
            "runtime": ENGINE_NAME,
        });
        println!("{}", serde_json::to_string(&payload)?);
        return Ok(());
    }

    eprintln!("{}", default_model);
    Ok(())
}