portail 2.1.0

Unified proxy/gateway: AI Gateway + MCP Gateway + CDN cache
Documentation
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
//! Godfather — System resource monitor + service health watchdog.
//!
//! Always-on background process that monitors disk, memory, CPU,
//! and portail process health every 10 seconds. Publishes events
//! and sends webhook alerts when thresholds are crossed. v0.6.

use crate::types::BoundedMeta;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;

// ── Helpers ──────────────────────────────────────────────────────

fn now_millis() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis() as u64
}

fn gf_disk_threshold() -> u8 {
    85
}
fn gf_memory_threshold() -> u8 {
    90
}
fn gf_min_free_disk() -> u64 {
    1_073_741_824
}

// ── Configuration ────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GodfatherConfig {
    pub enabled: bool,
    pub heartbeat_interval_secs: u64,
    pub check_services: bool,
    pub check_resources: bool,
    pub check_network: bool,
    #[serde(default)]
    pub thresholds: ResourceThresholds,
    #[serde(default)]
    pub alert_webhook_url: Option<String>,
}

impl Default for GodfatherConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            heartbeat_interval_secs: 10,
            check_services: true,
            check_resources: true,
            check_network: true,
            thresholds: ResourceThresholds::default(),
            alert_webhook_url: None,
        }
    }
}

// ── Service Status ───────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ServiceStatus {
    pub name: String,
    pub status: ServiceState,
    pub pid: Option<u32>,
    pub uptime_secs: Option<u64>,
    pub memory_bytes: Option<u64>,
    pub last_heartbeat: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ServiceState {
    Running,
    Degraded,
    Stopped,
    Unknown,
}

// ── Resource Stats ───────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceThresholds {
    #[serde(default = "gf_disk_threshold")]
    pub disk_usage_pct: u8,
    #[serde(default = "gf_memory_threshold")]
    pub memory_usage_pct: u8,
    #[serde(default = "gf_min_free_disk")]
    pub min_free_disk_bytes: u64,
}

impl Default for ResourceThresholds {
    fn default() -> Self {
        Self {
            disk_usage_pct: gf_disk_threshold(),
            memory_usage_pct: gf_memory_threshold(),
            min_free_disk_bytes: gf_min_free_disk(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceStats {
    pub memory_used_bytes: u64,
    pub memory_total_bytes: u64,
    pub memory_usage_pct: f64,
    pub cpu_usage_pct: f64,
    pub disk_used_bytes: u64,
    pub disk_total_bytes: u64,
    pub disk_usage_pct: f64,
    pub system_uptime_secs: u64,
    pub process_memory_bytes: u64,
    pub process_cpu_pct: f64,
    pub open_files: u64,
    pub open_connections: u64,
}

// ── Network Stats ────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct NetworkStats {
    pub active_connections: u64,
    pub total_requests: u64,
    pub total_errors: u64,
    pub bytes_in: u64,
    pub bytes_out: u64,
    pub dns_queries: u64,
    pub dns_failures: u64,
}

// ── Godfather Core ───────────────────────────────────────────────

pub struct Godfather {
    _config: GodfatherConfig,
    started_at: Instant,
    tick_count: AtomicU64,
    services: std::sync::RwLock<Vec<ServiceStatus>>,
}

impl Godfather {
    pub fn new(config: GodfatherConfig) -> Self {
        Self {
            _config: config,
            started_at: Instant::now(),
            tick_count: AtomicU64::new(0),
            services: std::sync::RwLock::new(Vec::new()),
        }
    }

    pub fn record_tick(&self) -> u64 {
        self.tick_count.fetch_add(1, Ordering::Relaxed) + 1
    }

    pub fn check_portail_services(&self, state: &crate::AppState) -> Vec<ServiceStatus> {
        let mut services = Vec::new();
        let elapsed = self.started_at.elapsed().as_secs();

        services.push(ServiceStatus {
            name: "proxy".into(),
            status: ServiceState::Running,
            pid: Some(std::process::id()),
            uptime_secs: Some(elapsed),
            memory_bytes: None,
            last_heartbeat: Some(now_millis()),
        });
        services.push(ServiceStatus {
            name: "event_log".into(),
            status: ServiceState::Running,
            pid: None,
            uptime_secs: Some(elapsed),
            memory_bytes: None,
            last_heartbeat: Some(now_millis()),
        });
        let _ = state; // reserve for future per-service checks
        services
    }

    pub fn update_service(&self, svc: ServiceStatus) {
        let mut list = self.services.write().unwrap();
        if let Some(existing) = list.iter_mut().find(|s| s.name == svc.name) {
            *existing = svc;
        } else {
            list.push(svc);
        }
    }

    pub fn gather_resources(&self) -> ResourceStats {
        use sysinfo::{Disks, System};

        let mut sys = System::new_all();
        sys.refresh_all();

        let mem_used = sys.used_memory();
        let mem_total = sys.total_memory();
        let mem_pct = if mem_total > 0 {
            (mem_used as f64 / mem_total as f64) * 100.0
        } else {
            0.0
        };

        let cpu_pct = sys.global_cpu_usage();
        let uptime = System::uptime();

        let pid = sysinfo::Pid::from_u32(std::process::id());
        let proc_mem = sys.process(pid).map(|p| p.memory()).unwrap_or(0);
        let proc_cpu = sys
            .process(pid)
            .map(|p| p.cpu_usage() as f64)
            .unwrap_or(0.0);

        let disks = Disks::new_with_refreshed_list();
        let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("/"));
        let mut disk_used: u64 = 0;
        let mut disk_total: u64 = 0;
        let mut disk_pct: f64 = 0.0;

        let mut best: Option<&sysinfo::Disk> = None;
        let mut best_len = 0;
        for disk in disks.list() {
            let mp = disk.mount_point().to_string_lossy();
            if cwd.to_string_lossy().starts_with(mp.as_ref()) && mp.len() > best_len {
                best = Some(disk);
                best_len = mp.len();
            }
        }
        if best.is_none() {
            for disk in disks.list() {
                if disk.mount_point().to_string_lossy() == "/" {
                    best = Some(disk);
                    break;
                }
            }
        }
        if let Some(disk) = best {
            disk_total = disk.total_space();
            let free = disk.available_space();
            disk_used = disk_total.saturating_sub(free);
            disk_pct = if disk_total > 0 {
                (disk_used as f64 / disk_total as f64) * 100.0
            } else {
                0.0
            };
        }

        ResourceStats {
            memory_used_bytes: mem_used,
            memory_total_bytes: mem_total,
            memory_usage_pct: mem_pct,
            cpu_usage_pct: cpu_pct as f64,
            disk_used_bytes: disk_used,
            disk_total_bytes: disk_total,
            disk_usage_pct: disk_pct,
            system_uptime_secs: uptime,
            process_memory_bytes: proc_mem,
            process_cpu_pct: proc_cpu,
            open_files: 0,
            open_connections: 0,
        }
    }

    pub fn gather_network(&self, state: &crate::AppState) -> NetworkStats {
        let trace_stats = state.trace_store.stats();
        NetworkStats {
            active_connections: 0,
            total_requests: trace_stats.total_traces as u64,
            total_errors: trace_stats.error_traces as u64,
            bytes_in: 0,
            bytes_out: 0,
            dns_queries: 0,
            dns_failures: 0,
        }
    }
}

// ── Background Runner ────────────────────────────────────────────

pub async fn run_godfather(config: GodfatherConfig, state: Arc<crate::AppState>) {
    let godfather = Godfather::new(config.clone());
    let interval = std::time::Duration::from_secs(config.heartbeat_interval_secs);

    tracing::info!(interval = %config.heartbeat_interval_secs, "Godfather started");

    state.event_log.publish(crate::events::AgentEvent {
        agent_id: "godfather".into(),
        event_type: "started".into(),
        severity: "info".into(),
        timestamp: 0,
        metadata: BoundedMeta::from_iter([
            ("version".into(), env!("CARGO_PKG_VERSION").into()),
            ("pid".into(), std::process::id().to_string()),
            (
                "interval".into(),
                config.heartbeat_interval_secs.to_string(),
            ),
        ]),
    });

    loop {
        tokio::time::sleep(interval).await;
        let tick = godfather.record_tick();

        // Resources — always checked (mandatory)
        {
            let resources = godfather.gather_resources();
            let thresholds = &config.thresholds;
            let mut severity = "info";
            let mut alerts: Vec<String> = Vec::new();
            let disk_free = resources
                .disk_total_bytes
                .saturating_sub(resources.disk_used_bytes);

            if resources.disk_usage_pct > thresholds.disk_usage_pct as f64 {
                severity = "critical";
                alerts.push(format!(
                    "disk {:.1}% > {}%",
                    resources.disk_usage_pct, thresholds.disk_usage_pct
                ));
            }
            if disk_free < thresholds.min_free_disk_bytes && resources.disk_total_bytes > 0 {
                severity = "critical";
                alerts.push(format!(
                    "disk free {} < min {}",
                    disk_free, thresholds.min_free_disk_bytes
                ));
            }
            if resources.memory_usage_pct > thresholds.memory_usage_pct as f64 {
                if severity != "critical" {
                    severity = "warning";
                }
                alerts.push(format!(
                    "memory {:.1}% > {}%",
                    resources.memory_usage_pct, thresholds.memory_usage_pct
                ));
            }

            let mut meta = BoundedMeta::from_iter([
                (
                    "memory_pct".into(),
                    format!("{:.1}", resources.memory_usage_pct),
                ),
                ("cpu_pct".into(), format!("{:.1}", resources.cpu_usage_pct)),
                (
                    "disk_pct".into(),
                    format!("{:.1}", resources.disk_usage_pct),
                ),
                ("disk_free_bytes".into(), disk_free.to_string()),
                (
                    "process_memory_bytes".into(),
                    resources.process_memory_bytes.to_string(),
                ),
                (
                    "process_cpu_pct".into(),
                    format!("{:.1}", resources.process_cpu_pct),
                ),
                (
                    "system_uptime_secs".into(),
                    resources.system_uptime_secs.to_string(),
                ),
            ]);
            if !alerts.is_empty() {
                let _ = meta.insert("alerts".into(), alerts.join("; "));
            }

            state.event_log.publish(crate::events::AgentEvent {
                agent_id: "godfather".into(),
                event_type: "resource".into(),
                severity: severity.into(),
                timestamp: 0,
                metadata: meta.clone(),
            });

            if severity == "critical" {
                if let Some(ref webhook_url) = config.alert_webhook_url {
                    let payload = serde_json::json!({
                        "text": format!("🚨 portail CRITICAL: {}", alerts.join(", ")),
                        "attachments": [{"title": "Resource Alert", "fields": [
                            {"title": "Disk", "value": format!("{:.1}% used, {:.1} GB free", resources.disk_usage_pct, disk_free as f64 / 1e9), "short": true},
                            {"title": "Memory", "value": format!("{:.1}% ({:.1}/{:.1} GB)", resources.memory_usage_pct, resources.memory_used_bytes as f64 / 1e9, resources.memory_total_bytes as f64 / 1e9), "short": true},
                            {"title": "CPU", "value": format!("sys {:.1}% / proc {:.1}%", resources.cpu_usage_pct, resources.process_cpu_pct), "short": true},
                            {"title": "Uptime", "value": format!("{}s", resources.system_uptime_secs), "short": true},
                        ]}]
                    });
                    let _ = reqwest::Client::new()
                        .post(webhook_url)
                        .json(&payload)
                        .timeout(std::time::Duration::from_secs(5))
                        .send()
                        .await;
                }
            }
        }

        // Services
        if config.check_services {
            let services = godfather.check_portail_services(&state);
            for svc in &services {
                godfather.update_service(svc.clone());
            }
            state.event_log.publish(crate::events::AgentEvent {
                agent_id: "godfather".into(),
                event_type: "heartbeat".into(),
                severity: "info".into(),
                timestamp: 0,
                metadata: BoundedMeta::from_iter([
                    ("tick".into(), tick.to_string()),
                    (
                        "uptime".into(),
                        godfather.started_at.elapsed().as_secs().to_string(),
                    ),
                    ("services".into(), services.len().to_string()),
                ]),
            });
        }

        if config.check_network {
            let network = godfather.gather_network(&state);
            state.event_log.publish(crate::events::AgentEvent {
                agent_id: "godfather".into(),
                event_type: "network".into(),
                severity: "info".into(),
                timestamp: 0,
                metadata: BoundedMeta::from_iter([
                    ("total_requests".into(), network.total_requests.to_string()),
                    ("total_errors".into(), network.total_errors.to_string()),
                ]),
            });
        }

        tracing::debug!(tick = %tick, uptime = %godfather.started_at.elapsed().as_secs(), "godfather heartbeat");
    }
}

// ── HTTP Handler ─────────────────────────────────────────────────

pub async fn handle_godfather_status(
    axum::extract::State(state): axum::extract::State<Arc<crate::AppState>>,
) -> axum::Json<GodfatherStatus> {
    let gf = Godfather::new(GodfatherConfig::default());
    axum::Json(GodfatherStatus {
        uptime_secs: gf.started_at.elapsed().as_secs(),
        services: gf.check_portail_services(&state),
        resources: gf.gather_resources(),
        network: gf.gather_network(&state),
    })
}

#[derive(Debug, Serialize)]
pub struct GodfatherStatus {
    pub uptime_secs: u64,
    pub services: Vec<ServiceStatus>,
    pub resources: ResourceStats,
    pub network: NetworkStats,
}

pub fn router() -> axum::Router<Arc<crate::AppState>> {
    axum::Router::new().route(
        "/godfather/status",
        axum::routing::get(handle_godfather_status),
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn godfather_config_defaults() {
        let cfg = GodfatherConfig::default();
        assert!(cfg.enabled);
        assert_eq!(cfg.heartbeat_interval_secs, 10);
        assert!(cfg.check_services);
        assert!(cfg.check_resources);
        assert!(cfg.check_network);
        assert!(cfg.alert_webhook_url.is_none());
    }

    #[test]
    fn resource_thresholds_defaults() {
        let thresholds = ResourceThresholds::default();
        assert_eq!(thresholds.disk_usage_pct, 85);
        assert_eq!(thresholds.memory_usage_pct, 90);
        assert_eq!(thresholds.min_free_disk_bytes, 1_073_741_824);
    }

    #[test]
    fn service_state_serialization() {
        let states = vec![
            (ServiceState::Running, "running"),
            (ServiceState::Degraded, "degraded"),
            (ServiceState::Stopped, "stopped"),
            (ServiceState::Unknown, "unknown"),
        ];
        for (state, expected) in states {
            let json = serde_json::to_string(&state).unwrap();
            assert!(
                json.contains(expected),
                "ServiceState {:?} should serialize to contain '{}'",
                state,
                expected
            );
        }
    }

    #[test]
    fn godfather_new_initializes_tick_count() {
        let cfg = GodfatherConfig::default();
        let gf = Godfather::new(cfg);
        assert_eq!(gf.record_tick(), 1);
        assert_eq!(gf.record_tick(), 2);
        assert_eq!(gf.record_tick(), 3);
    }

    #[test]
    fn godfather_record_tick_is_monotonic() {
        let cfg = GodfatherConfig::default();
        let gf = Godfather::new(cfg);
        let prev = gf.record_tick();
        let next = gf.record_tick();
        assert!(next > prev);
    }

    #[test]
    fn godfather_update_service_adds_and_updates() {
        let cfg = GodfatherConfig::default();
        let gf = Godfather::new(cfg);

        let svc = ServiceStatus {
            name: "test-service".into(),
            status: ServiceState::Running,
            pid: Some(1234),
            uptime_secs: Some(60),
            memory_bytes: Some(1024),
            last_heartbeat: Some(1000),
        };

        gf.update_service(svc.clone());
        let updated = ServiceStatus {
            name: "test-service".into(),
            status: ServiceState::Degraded,
            ..svc
        };
        gf.update_service(updated);
    }

    #[test]
    fn resource_stats_serialization() {
        let stats = ResourceStats {
            memory_used_bytes: 10_000_000,
            memory_total_bytes: 16_000_000,
            memory_usage_pct: 62.5,
            cpu_usage_pct: 15.3,
            disk_used_bytes: 50_000_000_000,
            disk_total_bytes: 100_000_000_000,
            disk_usage_pct: 50.0,
            system_uptime_secs: 86400,
            process_memory_bytes: 5_000_000,
            process_cpu_pct: 2.1,
            open_files: 128,
            open_connections: 4,
        };
        let json = serde_json::to_string(&stats).unwrap();
        assert!(json.contains("memory_used_bytes"));
        assert!(json.contains("cpu_usage_pct"));
        let deser: ResourceStats = serde_json::from_str(&json).unwrap();
        assert_eq!(deser.memory_used_bytes, 10_000_000);
        assert_eq!(deser.cpu_usage_pct, 15.3);
    }

    #[test]
    fn network_stats_serialization() {
        let stats = NetworkStats {
            active_connections: 42,
            total_requests: 1000,
            total_errors: 5,
            bytes_in: 500_000,
            bytes_out: 2_000_000,
            dns_queries: 100,
            dns_failures: 2,
        };
        let json = serde_json::to_string(&stats).unwrap();
        assert!(json.contains("active_connections"));
        let deser: NetworkStats = serde_json::from_str(&json).unwrap();
        assert_eq!(deser.active_connections, 42);
    }
}