gioser
⎇ ca75ba185f880d431eb118250d3931a91d354b2e crates/ente-zero/src/main.rs Rust · 454 lines · 16.8 KB History Who touched what raw

Cristales temporales, replay, lease/renew y audit streaming ca75ba185 · Sergio ·

1//! Ente #0 — el primer Ente. PID 1 del fractal.
2//!
3//! Reglas no negociables:
4//!   1. NUNCA lógica de servicio aquí. Sólo: leer Semilla, cosechar zombis,
5//!      mediar capacidades, propagar eventos.
6//!   2. Single-threaded. Cualquier paralelismo se delega a Entes worker.
7//!      Un panic en un thread de PID 1 = kernel panic.
8//!   3. Errores de hijos son *eventos* en `graph_tx`, no `Result` propagado.
9//!
10//! Este archivo es sólo wireup. La lógica vive en:
11//!   - `seed`        : construcción/restauración de la Tarjeta Semilla
12//!   - `bus`         : listener Unix + auth via SO_PEERCRED
13//!   - `graph::*`    : estado del fractal (lifecycle, topology, shutdown,
14//!                     bus_mediator, devices, capabilities)
15//!   - `events`      : tipos de eventos del bucle primordial
16//!   - crates externos del workspace para CAS, soma, wasm, snapshot, kernel.
17
18mod brain_glue;
19mod bus;
20mod events;
21mod graph;
22mod seed;
23
24use anyhow::Context;
25use ente_brain::{BrainState, IntrospectServer};
26use ente_kernel::{become_child_subreaper, bootstrap_kernel_surface, spawn_sigchld_stream, spawn_uevent_stream};
27use events::{ExitStatus, GraphEvent, ShutdownReason};
28use graph::EnteGraph;
29use nix::errno::Errno;
30use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus};
31use nix::unistd::{getpid, Pid};
32use std::path::PathBuf;
33use std::time::Duration;
34use tokio::sync::mpsc;
35use tracing::{error, info, warn};
36
37struct CliArgs {
38    checkpoint: Option<PathBuf>,
39    restore: Option<PathBuf>,
40    rules: Option<PathBuf>,
41    rules_out: Option<PathBuf>,
42    audit_head: Option<PathBuf>,
43    metrics_addr: Option<String>,
44    brain_half_life: Option<f64>,
45    autopromote_secs: Option<u64>,
46}
47
48fn parse_args() -> CliArgs {
49    let mut args = std::env::args().skip(1);
50    let mut checkpoint = None;
51    let mut restore = None;
52    let mut rules = None;
53    let mut rules_out = None;
54    let mut audit_head = None;
55    let mut metrics_addr = None;
56    let mut brain_half_life = None;
57    let mut autopromote_secs = None;
58    while let Some(a) = args.next() {
59        match a.as_str() {
60            "--checkpoint" => checkpoint = args.next().map(PathBuf::from),
61            "--restore" => restore = args.next().map(PathBuf::from),
62            "--rules" => rules = args.next().map(PathBuf::from),
63            "--rules-out" => rules_out = args.next().map(PathBuf::from),
64            "--audit-head" => audit_head = args.next().map(PathBuf::from),
65            "--metrics-addr" => metrics_addr = args.next(),
66            "--brain-half-life" => brain_half_life = args.next().and_then(|s| s.parse().ok()),
67            "--autopromote-secs" => autopromote_secs = args.next().and_then(|s| s.parse().ok()),
68            other => warn!(arg = %other, "argumento desconocido, ignorado"),
69        }
70    }
71    CliArgs {
72        checkpoint, restore, rules, rules_out, audit_head,
73        metrics_addr, brain_half_life, autopromote_secs,
74    }
75}
76
77fn main() -> anyhow::Result<()> {
78    init_tracing();
79    let cli = parse_args();
80    let pid = getpid();
81    let dev_mode = pid != Pid::from_raw(1);
82
83    if dev_mode {
84        warn!(?pid, "ente-zero corriendo en DEV MODE (no PID 1) — kernel surface no se monta");
85    } else {
86        info!("ente-zero despierta como PID 1");
87        bootstrap_kernel_surface().context("bootstrap kernel surface")?;
88        become_child_subreaper().context("PR_SET_CHILD_SUBREAPER")?;
89    }
90
91    let card = seed::load(dev_mode, cli.restore.as_deref())?;
92
93    // current_thread runtime: ver doctrina al inicio del módulo.
94    let rt = tokio::runtime::Builder::new_current_thread()
95        .enable_io()
96        .enable_time()
97        .build()?;
98
99    rt.block_on(primordial_loop(
100        card, dev_mode,
101        cli.checkpoint, cli.rules, cli.rules_out,
102        cli.audit_head, cli.metrics_addr, cli.brain_half_life,
103        cli.autopromote_secs,
104    ))
105}
106
107async fn primordial_loop(
108    seed_card: ente_card::EntityCard,
109    dev_mode: bool,
110    checkpoint_path: Option<PathBuf>,
111    rules_path: Option<PathBuf>,
112    rules_out: Option<PathBuf>,
113    audit_head: Option<PathBuf>,
114    metrics_addr: Option<String>,
115    brain_half_life: Option<f64>,
116    autopromote_secs: Option<u64>,
117) -> anyhow::Result<()> {
118    info!(seed_id = %seed_card.id, label = %seed_card.label, "Ente #0 entra al bucle primordial");
119
120    let (graph_tx, mut graph_rx) = mpsc::channel::<GraphEvent>(64);
121    let mut sigchld = spawn_sigchld_stream()?;
122    // Uevents puede fallar en dev (sin CAP_NET_ADMIN). Degradamos a un
123    // canal nunca-listo en lugar de abortar el bucle primordial.
124    let mut uevents = match spawn_uevent_stream() {
125        Ok(rx) => rx,
126        Err(e) => {
127            warn!(?e, "uevents deshabilitados (probablemente falta CAP_NET_ADMIN)");
128            let (_keep_tx, rx) = mpsc::channel::<ente_kernel::UEvent>(1);
129            std::mem::forget(_keep_tx);
130            rx
131        }
132    };
133
134    // Bus interno: listener antes de spawn de hijos para que su Announce
135    // tenga adónde llegar. Su path se inyecta en ENTE_BUS_SOCK por soma.
136    let bus_sock = bus::default_socket_path();
137    let bus_path = bus::spawn_bus(bus_sock, graph_tx.clone())?;
138    ente_soma::set_bus_sock(bus_path.to_string_lossy().into_owned());
139
140    let mut graph = EnteGraph::new(seed_card);
141    graph.instantiate_seed_dependencies(&graph_tx).await?;
142
143    // Cerebro: BrainState compartido + servidor de introspección.
144    // Window de 1024 eventos — suficiente para correlaciones interesantes
145    // sin gastar memoria de PID 1. En dev bajamos el umbral de cristalización
146    // para que el demo (pocos eventos) produzca cristales observables.
147    let mut brain = if dev_mode {
148        // Umbrales relajados para que el demo (pocos eventos) produzca
149        // cristales observables. Con P(b|a) normalizada a [0,1], los
150        // valores típicos en muestras pequeñas son 0.2-0.5.
151        BrainState::with_params(1024, ente_brain::CrystallizationParams {
152            min_support: 2,
153            min_conditional_prob: 0.3,
154            min_pmi: 1.0,
155        })
156    } else {
157        BrainState::new(1024)
158    };
159    if let Some(out_path) = rules_out {
160        brain = brain.with_rules_out(out_path);
161    }
162    if let Some(hl) = brain_half_life {
163        let mut obs = brain.observer.write().await;
164        // Reemplazar con un observer nuevo que tenga half-life. Estado
165        // anterior (vacío en este punto) descartado.
166        *obs = ente_brain::Observer::new(1024).with_half_life(hl);
167        info!(hl_secs = hl, "observer con time-decay activo");
168    }
169    if let Some(secs) = autopromote_secs {
170        ente_brain::spawn_autopromote_loop(
171            brain.clone(),
172            ente_brain::AutopromoteParams {
173                interval_secs: secs,
174                threshold: brain.params, // mismo threshold que crystals manuales
175            },
176        );
177    }
178    // Si --audit-head, configuramos el head pointer y arrancamos auto-flush.
179    if let Some(head_path) = audit_head {
180        // Re-creamos el AuditLog con head pointer.
181        let new_audit = ente_brain::audit::AuditLog::new()
182            .with_head_pointer(head_path);
183        *brain.audit.write().await = new_audit;
184        spawn_audit_auto_flush(brain.clone());
185    }
186
187    // Carga inicial de reglas vía KCL o JSON, si --rules path proporcionado.
188    if let Some(path) = &rules_path {
189        match ente_brain::load_rules_file(path) {
190            Ok(rules) => {
191                let mut engine = brain.engine.write().await;
192                for r in rules {
193                    engine.insert(r);
194                }
195                info!(count = engine.len(), path = %path.display(), "reglas cargadas");
196            }
197            Err(e) => warn!(?e, path = %path.display(), "carga de reglas falló"),
198        }
199    }
200
201    // Endpoint Prometheus opcional. En dev por defecto en 127.0.0.1:9911 si
202    // el flag no se pasó.
203    let metrics_addr = metrics_addr.or_else(|| {
204        if dev_mode { Some("127.0.0.1:9911".to_string()) } else { None }
205    });
206    if let Some(addr_s) = metrics_addr {
207        match addr_s.parse::<std::net::SocketAddr>() {
208            Ok(addr) => {
209                let s = brain.clone();
210                tokio::spawn(async move {
211                    if let Err(e) = ente_brain::serve_metrics(s, addr).await {
212                        warn!(?e, "metrics server cayó");
213                    }
214                });
215            }
216            Err(e) => warn!(?e, addr = %addr_s, "metrics-addr inválido"),
217        }
218    }
219    spawn_brain_introspect(brain.clone());
220    let brain_sink = brain_glue::GraphSink {
221        graph_tx: graph_tx.clone(),
222        // Spawns auto-disparados desde reglas usan la identidad de la Semilla
223        // (único Ente con Capability::Spawn por construcción).
224        requester: graph.seed_id(),
225    };
226
227    // Demo automático del forwarding (sólo dev, sólo si el binario existe).
228    if dev_mode && std::path::Path::new("target/debug/ente-echo").exists() {
229        spawn_echo_smoke_test(bus_path.clone());
230    }
231
232    // En dev mode no tenemos hijos por defecto y el bucle se quedaría inerte.
233    let dev_exit = if dev_mode {
234        Some(tokio::time::sleep(Duration::from_secs(4)))
235    } else {
236        None
237    };
238    tokio::pin!(dev_exit);
239
240    // GC de capability grants expirados — corre cada 10 segundos.
241    let mut grant_purge = tokio::time::interval(Duration::from_secs(10));
242    grant_purge.tick().await; // descartar primer tick inmediato
243
244    loop {
245        tokio::select! {
246            biased;
247
248            Some(_) = sigchld.recv() => {
249                reap_until_empty(&mut graph, &graph_tx).await;
250            }
251
252            Some(uevt) = uevents.recv() => {
253                graph.on_uevent(uevt, &graph_tx).await;
254            }
255
256            Some(evt) = graph_rx.recv() => {
257                // Cerebro observa antes que el grafo mute. Snapshot del
258                // SubjectInfo se hace contra el estado pre-mutación.
259                feed_brain(&brain, &brain_sink, &graph, &evt).await;
260                if dispatch_graph_event(&mut graph, evt, &graph_tx, &checkpoint_path).await {
261                    return Ok(());
262                }
263            }
264
265            _ = grant_purge.tick() => {
266                let n = graph.purge_expired_grants();
267                if n > 0 {
268                    info!(purged = n, active = graph.active_grants_count(), "GC capability grants");
269                }
270            }
271
272            _ = async { dev_exit.as_mut().as_pin_mut().unwrap().await }, if dev_mode => {
273                info!("dev mode: timer expirado, cerrando bucle primordial");
274                let _ = graph_tx.send(GraphEvent::Shutdown {
275                    reason: ShutdownReason::SeedRequested,
276                }).await;
277            }
278        }
279    }
280}
281
282/// Devuelve `true` si el bucle primordial debe terminar.
283async fn dispatch_graph_event(
284    graph: &mut EnteGraph,
285    evt: GraphEvent,
286    tx: &mpsc::Sender<GraphEvent>,
287    checkpoint: &Option<PathBuf>,
288) -> bool {
289    match evt {
290        GraphEvent::EnteDied { id, status } => {
291            graph.on_death(id, status, tx).await;
292        }
293        GraphEvent::CapabilityRequested { from, cap, reply } => {
294            graph.mediate_capability(from, cap, reply).await;
295        }
296        GraphEvent::SpawnRequest { card, requester } => {
297            if let Err(e) = graph.authorize_and_spawn(card, requester).await {
298                warn!(?e, "spawn request error");
299            }
300        }
301        GraphEvent::BusRequest { peer, from, request, outbound, reply } => {
302            graph.on_bus_request(peer, from, request, outbound, reply).await;
303        }
304        GraphEvent::BusResponse { seq, response } => {
305            graph.on_bus_response(seq, response).await;
306        }
307        GraphEvent::BusConnClosed { ente_id } => {
308            graph.on_bus_conn_closed(ente_id).await;
309        }
310        GraphEvent::Shutdown { reason } => {
311            warn!(?reason, "shutdown del fractal");
312            if let Some(path) = checkpoint.as_ref() {
313                let snap = graph.snapshot();
314                match snap.write(path) {
315                    Ok(()) => info!(path = %path.display(), entes = snap.entes.len(), "snapshot persistido"),
316                    Err(e) => warn!(?e, "snapshot write falló"),
317                }
318            }
319            graph.cascade_shutdown().await;
320            return true;
321        }
322    }
323    false
324}
325
326async fn reap_until_empty(graph: &mut EnteGraph, tx: &mpsc::Sender<GraphEvent>) {
327    loop {
328        match waitpid(None, Some(WaitPidFlag::WNOHANG)) {
329            Ok(WaitStatus::StillAlive) => return,
330            Ok(WaitStatus::Exited(pid, code)) => {
331                emit_death(graph, tx, pid, ExitStatus::Exit(code)).await;
332            }
333            Ok(WaitStatus::Signaled(pid, sig, _core)) => {
334                emit_death(graph, tx, pid, ExitStatus::Killed(sig)).await;
335            }
336            Ok(_) => continue, // Stopped/Continued — irrelevantes
337            Err(Errno::ECHILD) => return,
338            Err(e) => {
339                error!(?e, "waitpid fallo no recuperable en bucle de reaping");
340                return;
341            }
342        }
343    }
344}
345
346async fn emit_death(
347    graph: &EnteGraph,
348    tx: &mpsc::Sender<GraphEvent>,
349    pid: Pid,
350    status: ExitStatus,
351) {
352    let id = match graph.lookup_pid(pid) {
353        Some(id) => id,
354        None => {
355            // Proceso adoptado (subreaper): no está en nuestro grafo.
356            info!(?pid, ?status, "huérfano cosechado (no en grafo)");
357            return;
358        }
359    };
360    let _ = tx.send(GraphEvent::EnteDied { id, status }).await;
361}
362
363fn spawn_echo_smoke_test(bus_path: PathBuf) {
364    tokio::spawn(async move {
365        tokio::time::sleep(Duration::from_millis(300)).await;
366        match ente_bus::BusClient::connect(&bus_path).await {
367            Ok(mut client) => {
368                let req = ente_bus::BusRequest::Invoke {
369                    cap: ente_echo::echo_capability(),
370                    blob: b"hola fractal forwardeado".to_vec(),
371                };
372                match client.call(req).await {
373                    Ok(ente_bus::BusResponse::Invoked { result }) => {
374                        info!(echo = %String::from_utf8_lossy(&result), "Invoke ECHO round-trip OK");
375                    }
376                    Ok(other) => warn!(?other, "Invoke ECHO respuesta inesperada"),
377                    Err(e) => warn!(?e, "Invoke ECHO falló"),
378                }
379            }
380            Err(e) => warn!(?e, "no se pudo conectar al bus para test"),
381        }
382    });
383}
384
385fn init_tracing() {
386    use tracing_subscriber::{fmt, EnvFilter};
387    let filter = EnvFilter::try_from_default_env()
388        .unwrap_or_else(|_| EnvFilter::new("ente_zero=debug,info"));
389    fmt().with_env_filter(filter).with_target(true).init();
390}
391
392fn brain_introspect_path() -> PathBuf {
393    if let Ok(p) = std::env::var("ENTE_BRAIN_SOCK") {
394        return p.into();
395    }
396    let runtime = std::env::var("XDG_RUNTIME_DIR")
397        .unwrap_or_else(|_| std::env::var("TMPDIR").unwrap_or_else(|_| "/tmp".into()));
398    format!("{runtime}/ente-brain.sock").into()
399}
400
401/// Auto-flush del audit log a CAS cada 10 segundos. Ejecuta best-effort:
402/// si el flush falla lo logeamos pero no abortamos. La integridad del log
403/// queda garantizada por su hash chain — re-flushar es idempotente.
404fn spawn_audit_auto_flush(state: BrainState) {
405    tokio::spawn(async move {
406        let mut tick = tokio::time::interval(std::time::Duration::from_secs(10));
407        tick.tick().await; // descartar primer tick inmediato
408        loop {
409            tick.tick().await;
410            let mut audit = state.audit.write().await;
411            match audit.flush_to_cas() {
412                Ok(0) => {} // nada nuevo
413                Ok(n) => info!(written = n, total = audit.flushed_count(), "audit auto-flush"),
414                Err(e) => warn!(?e, "audit auto-flush falló"),
415            }
416        }
417    });
418}
419
420fn spawn_brain_introspect(state: BrainState) {
421    let path = brain_introspect_path();
422    tokio::spawn(async move {
423        let server = IntrospectServer::new(state);
424        if let Err(e) = server.serve(&path).await {
425            warn!(?e, "introspect server cayó");
426        }
427    });
428}
429
430/// Registra el evento en el observer y dispatcha cualquier regla matched.
431/// Para reglas Sequence: pasamos los últimos N eventos del observer como
432/// history al engine.
433async fn feed_brain(
434    brain: &BrainState,
435    sink: &brain_glue::GraphSink,
436    graph: &EnteGraph,
437    evt: &GraphEvent,
438) {
439    let Some((kind, subj)) = brain_glue::graph_event_to_brain(evt, graph) else { return };
440    let history: Vec<ente_brain::TimedEvent> = {
441        let mut obs = brain.observer.write().await;
442        obs.record(kind.clone());
443        // Snapshot de los últimos 16 eventos — suficiente para cualquier
444        // Sequence pattern razonable. Clone hace una sola alocación.
445        obs.recent(16).cloned().collect()
446    };
447    let rules = {
448        let engine = brain.engine.read().await;
449        engine.dispatch(&kind, &subj, &history)
450    };
451    if !rules.is_empty() {
452        ente_brain::dispatch_actions(&rules, sink).await;
453    }
454}