⎇ badf4257ecd5a4ef06f6f477279834893a24f87b
crates/ente-zero/src/main.rs
History
Who touched what
raw
audit-verify, autopromote, histogramas y hot-reload caps
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 loop {
241 tokio::select! {
242 biased;
243
244 Some(_) = sigchld.recv() => {
245 reap_until_empty(&mut graph, &graph_tx).await;
246 }
247
248 Some(uevt) = uevents.recv() => {
249 graph.on_uevent(uevt, &graph_tx).await;
250 }
251
252 Some(evt) = graph_rx.recv() => {
253 // Cerebro observa antes que el grafo mute. Snapshot del
254 // SubjectInfo se hace contra el estado pre-mutación.
255 feed_brain(&brain, &brain_sink, &graph, &evt).await;
256 if dispatch_graph_event(&mut graph, evt, &graph_tx, &checkpoint_path).await {
257 return Ok(());
258 }
259 }
260
261 _ = async { dev_exit.as_mut().as_pin_mut().unwrap().await }, if dev_mode => {
262 info!("dev mode: timer expirado, cerrando bucle primordial");
263 let _ = graph_tx.send(GraphEvent::Shutdown {
264 reason: ShutdownReason::SeedRequested,
265 }).await;
266 }
267 }
268 }
269}
270
271/// Devuelve `true` si el bucle primordial debe terminar.
272async fn dispatch_graph_event(
273 graph: &mut EnteGraph,
274 evt: GraphEvent,
275 tx: &mpsc::Sender<GraphEvent>,
276 checkpoint: &Option<PathBuf>,
277) -> bool {
278 match evt {
279 GraphEvent::EnteDied { id, status } => {
280 graph.on_death(id, status, tx).await;
281 }
282 GraphEvent::CapabilityRequested { from, cap, reply } => {
283 graph.mediate_capability(from, cap, reply).await;
284 }
285 GraphEvent::SpawnRequest { card, requester } => {
286 if let Err(e) = graph.authorize_and_spawn(card, requester).await {
287 warn!(?e, "spawn request error");
288 }
289 }
290 GraphEvent::BusRequest { peer, from, request, outbound, reply } => {
291 graph.on_bus_request(peer, from, request, outbound, reply).await;
292 }
293 GraphEvent::BusResponse { seq, response } => {
294 graph.on_bus_response(seq, response).await;
295 }
296 GraphEvent::BusConnClosed { ente_id } => {
297 graph.on_bus_conn_closed(ente_id).await;
298 }
299 GraphEvent::Shutdown { reason } => {
300 warn!(?reason, "shutdown del fractal");
301 if let Some(path) = checkpoint.as_ref() {
302 let snap = graph.snapshot();
303 match snap.write(path) {
304 Ok(()) => info!(path = %path.display(), entes = snap.entes.len(), "snapshot persistido"),
305 Err(e) => warn!(?e, "snapshot write falló"),
306 }
307 }
308 graph.cascade_shutdown().await;
309 return true;
310 }
311 }
312 false
313}
314
315async fn reap_until_empty(graph: &mut EnteGraph, tx: &mpsc::Sender<GraphEvent>) {
316 loop {
317 match waitpid(None, Some(WaitPidFlag::WNOHANG)) {
318 Ok(WaitStatus::StillAlive) => return,
319 Ok(WaitStatus::Exited(pid, code)) => {
320 emit_death(graph, tx, pid, ExitStatus::Exit(code)).await;
321 }
322 Ok(WaitStatus::Signaled(pid, sig, _core)) => {
323 emit_death(graph, tx, pid, ExitStatus::Killed(sig)).await;
324 }
325 Ok(_) => continue, // Stopped/Continued — irrelevantes
326 Err(Errno::ECHILD) => return,
327 Err(e) => {
328 error!(?e, "waitpid fallo no recuperable en bucle de reaping");
329 return;
330 }
331 }
332 }
333}
334
335async fn emit_death(
336 graph: &EnteGraph,
337 tx: &mpsc::Sender<GraphEvent>,
338 pid: Pid,
339 status: ExitStatus,
340) {
341 let id = match graph.lookup_pid(pid) {
342 Some(id) => id,
343 None => {
344 // Proceso adoptado (subreaper): no está en nuestro grafo.
345 info!(?pid, ?status, "huérfano cosechado (no en grafo)");
346 return;
347 }
348 };
349 let _ = tx.send(GraphEvent::EnteDied { id, status }).await;
350}
351
352fn spawn_echo_smoke_test(bus_path: PathBuf) {
353 tokio::spawn(async move {
354 tokio::time::sleep(Duration::from_millis(300)).await;
355 match ente_bus::BusClient::connect(&bus_path).await {
356 Ok(mut client) => {
357 let req = ente_bus::BusRequest::Invoke {
358 cap: ente_echo::echo_capability(),
359 blob: b"hola fractal forwardeado".to_vec(),
360 };
361 match client.call(req).await {
362 Ok(ente_bus::BusResponse::Invoked { result }) => {
363 info!(echo = %String::from_utf8_lossy(&result), "Invoke ECHO round-trip OK");
364 }
365 Ok(other) => warn!(?other, "Invoke ECHO respuesta inesperada"),
366 Err(e) => warn!(?e, "Invoke ECHO falló"),
367 }
368 }
369 Err(e) => warn!(?e, "no se pudo conectar al bus para test"),
370 }
371 });
372}
373
374fn init_tracing() {
375 use tracing_subscriber::{fmt, EnvFilter};
376 let filter = EnvFilter::try_from_default_env()
377 .unwrap_or_else(|_| EnvFilter::new("ente_zero=debug,info"));
378 fmt().with_env_filter(filter).with_target(true).init();
379}
380
381fn brain_introspect_path() -> PathBuf {
382 if let Ok(p) = std::env::var("ENTE_BRAIN_SOCK") {
383 return p.into();
384 }
385 let runtime = std::env::var("XDG_RUNTIME_DIR")
386 .unwrap_or_else(|_| std::env::var("TMPDIR").unwrap_or_else(|_| "/tmp".into()));
387 format!("{runtime}/ente-brain.sock").into()
388}
389
390/// Auto-flush del audit log a CAS cada 10 segundos. Ejecuta best-effort:
391/// si el flush falla lo logeamos pero no abortamos. La integridad del log
392/// queda garantizada por su hash chain — re-flushar es idempotente.
393fn spawn_audit_auto_flush(state: BrainState) {
394 tokio::spawn(async move {
395 let mut tick = tokio::time::interval(std::time::Duration::from_secs(10));
396 tick.tick().await; // descartar primer tick inmediato
397 loop {
398 tick.tick().await;
399 let mut audit = state.audit.write().await;
400 match audit.flush_to_cas() {
401 Ok(0) => {} // nada nuevo
402 Ok(n) => info!(written = n, total = audit.flushed_count(), "audit auto-flush"),
403 Err(e) => warn!(?e, "audit auto-flush falló"),
404 }
405 }
406 });
407}
408
409fn spawn_brain_introspect(state: BrainState) {
410 let path = brain_introspect_path();
411 tokio::spawn(async move {
412 let server = IntrospectServer::new(state);
413 if let Err(e) = server.serve(&path).await {
414 warn!(?e, "introspect server cayó");
415 }
416 });
417}
418
419/// Registra el evento en el observer y dispatcha cualquier regla matched.
420/// Para reglas Sequence: pasamos los últimos N eventos del observer como
421/// history al engine.
422async fn feed_brain(
423 brain: &BrainState,
424 sink: &brain_glue::GraphSink,
425 graph: &EnteGraph,
426 evt: &GraphEvent,
427) {
428 let Some((kind, subj)) = brain_glue::graph_event_to_brain(evt, graph) else { return };
429 let history: Vec<ente_brain::TimedEvent> = {
430 let mut obs = brain.observer.write().await;
431 obs.record(kind.clone());
432 // Snapshot de los últimos 16 eventos — suficiente para cualquier
433 // Sequence pattern razonable. Clone hace una sola alocación.
434 obs.recent(16).cloned().collect()
435 };
436 let rules = {
437 let engine = brain.engine.read().await;
438 engine.dispatch(&kind, &subj, &history)
439 };
440 if !rules.is_empty() {
441 ente_brain::dispatch_actions(&rules, sink).await;
442 }
443}