File size: 5,525 Bytes
4ae5926
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// main.rs β€” magmad sovereign agentic daemon
//
// Starts REST + SSE on :3000 (primary interface).
// gRPC on :50051 is defined in grpc/mod.rs and will be wired when protoc
// becomes available (install protoc, re-add tonic_build to build.rs).
//
// Usage:
//   magmad                         # start with defaults
//   MAGMA_HTTP_PORT=8080 magmad    # override HTTP port
//   MAGMA_NATS_URL=nats://... magmad
//
// Ahmad Ali Parr Β· SnapKitty Collective Β· 2026

mod bob;
mod config;
mod errant_ffi;
mod grpc;
mod http;
mod nats_bus;

use std::sync::Arc;
use anyhow::Result;
use tracing::{info, warn, error};

#[tokio::main]
async fn main() -> Result<()> {
    let cfg = config::Config::from_env();

    // ── Tracing ───────────────────────────────────────────────────────────
    tracing_subscriber::fmt()
        .with_env_filter(&cfg.log_level)
        .json()
        .init();

    print_banner(&cfg);

    // ── Verify liberrant is linked and working ────────────────────────────
    {
        let check = errant_ffi::verify_named(&["PUSH_UN", "SEAL"]);
        if check.ok {
            info!("liberrant {} online Β· FIPS SHA-256 verified", errant_ffi::version());
        } else {
            error!("liberrant self-check FAILED: {}", check.error);
            std::process::exit(1);
        }
    }

    // ── NATS connection (optional β€” daemon runs without it) ───────────────
    let nats = match nats_bus::NatsBus::connect(&cfg.nats_url).await {
        Ok(bus) => {
            info!("NATS connected: {}", cfg.nats_url);
            Some(Arc::new(bus))
        }
        Err(e) => {
            warn!("NATS unavailable ({}): running in local mode", e);
            None
        }
    };

    // ── Build shared HTTP state ───────────────────────────────────────────
    let http_state = http::AppState {
        config:   Arc::new(cfg.clone()),
        nats:     nats.clone(),
        worm_url: format!("{}/api/labs/ledge/seal", cfg.os_url),
    };

    // ── REST + SSE server (primary interface) ─────────────────────────────
    let http_addr: std::net::SocketAddr = cfg.http_addr().parse()?;
    let app = http::router(http_state)
        .layer(tower_http::cors::CorsLayer::permissive())
        .layer(tower_http::trace::TraceLayer::new_for_http());

    info!("REST/SSE listening on http://{}", http_addr);
    info!("gRPC endpoint defined at :{}  (enable: install protoc, run cargo build)", cfg.grpc_port);
    info!("magmad online Β· EVIDENCE OR SILENCE Β· Ξ©β†ΊΞ¨β†ΊΞ”β†ΊΞ›β†ΊΞ£β†ΊΞ¦β†ΊΞ±");

    let listener = tokio::net::TcpListener::bind(http_addr).await?;

    tokio::select! {
        r = axum::serve(listener, app) => {
            if let Err(e) = r { error!("HTTP server error: {e}"); }
        }
        _ = tokio::signal::ctrl_c() => {
            info!("Ξ© β€” magmad shutting down");
        }
    }

    Ok(())
}

fn print_banner(cfg: &config::Config) {
    eprintln!(r#"

╔══════════════════════════════════════════════════════════════╗

β•‘  β–ˆβ–ˆβ–ˆβ•—   β–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ•—   β–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—    β•‘

β•‘  β–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β•β•β• β–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—   β•‘

β•‘  β–ˆβ–ˆβ•”β–ˆβ–ˆβ–ˆβ–ˆβ•”β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β–ˆβ–ˆβ–ˆβ–ˆβ•”β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘   β•‘

β•‘  β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘   β•‘

β•‘  β–ˆβ–ˆβ•‘ β•šβ•β• β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘ β•šβ•β• β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•   β•‘

β•‘  β•šβ•β•     β•šβ•β•β•šβ•β•  β•šβ•β• β•šβ•β•β•β•β•β• β•šβ•β•     β•šβ•β•β•šβ•β•  β•šβ•β•β•šβ•β•β•β•β•β•    β•‘

╠══════════════════════════════════════════════════════════════╣

β•‘  Sovereign Agentic Daemon v{}                            β•‘

β•‘  BOB: TRUST-DEED-GATE β†’ MAMBA β†’ WATSON β†’ PROLOG β†’ SEAL     β•‘

╠══════════════════════════════════════════════════════════════╣

β•‘  REST/SSE  http://0.0.0.0:{}                               β•‘

β•‘  gRPC      :{}  (stub β€” install protoc to activate)      β•‘

β•‘  NATS      {}

β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

    "#,
    env!("CARGO_PKG_VERSION"),
    cfg.http_port,
    cfg.grpc_port,
    cfg.nats_url,
    );
}