Skip to main content

urge_monitor/
engine.rs

1//! The continuous governance monitor — ties obligations and temporal monitoring
2//! to the instantaneous pipeline.
3
4use crate::{
5    obligation::{Obligation, ObligationEvent, ObligationManager, ObligationViolationEvent},
6    temporal::TemporalMonitor,
7};
8use urge_meta::GovernancePipeline;
9
10#[cfg(feature = "alloc")]
11use alloc::vec::Vec;
12
13/// The full continuous governance engine.
14///
15/// This is the system-level entry point for long-running governance over
16/// event streams. Combine with your event bus or message queue.
17pub struct GovernanceMonitor {
18    pub pipeline: GovernancePipeline,
19    pub obligations: ObligationManager,
20    #[cfg(feature = "alloc")]
21    pub temporal_monitors: Vec<TemporalMonitor>,
22    #[cfg(not(feature = "alloc"))]
23    pub temporal_monitors: heapless::Vec<TemporalMonitor, 32>,
24    pub current_time: u64,
25}
26
27impl GovernanceMonitor {
28    pub fn new(pipeline: GovernancePipeline) -> Self {
29        GovernanceMonitor {
30            pipeline,
31            obligations: ObligationManager::new(),
32            #[cfg(feature = "alloc")]
33            temporal_monitors: Vec::new(),
34            #[cfg(not(feature = "alloc"))]
35            temporal_monitors: heapless::Vec::new(),
36            current_time: 0,
37        }
38    }
39
40    /// Advance logical time and process all deadline checks.
41    #[cfg(feature = "alloc")]
42    pub fn tick(&mut self, now: u64) -> Vec<ObligationViolationEvent> {
43        self.current_time = now;
44        self.obligations.process(ObligationEvent::TimeTick { now })
45    }
46
47    /// Register a new obligation to be tracked.
48    pub fn track_obligation(&mut self, ob: Obligation) {
49        let now = self.current_time;
50        self.obligations.register(ob, now);
51    }
52
53    /// Notify that an action was completed (may satisfy obligations).
54    #[cfg(feature = "alloc")]
55    pub fn action_completed(&mut self, agent: &str, action: &str) -> Vec<ObligationViolationEvent> {
56        let mut ag = heapless::String::new();
57        let mut ac = heapless::String::new();
58        for c in agent.chars().take(32) {
59            let _ = ag.push(c);
60        }
61        for c in action.chars().take(64) {
62            let _ = ac.push(c);
63        }
64        self.obligations.process(ObligationEvent::ActionCompleted {
65            agent: ag,
66            action: ac,
67            timestamp: self.current_time,
68        })
69    }
70
71    /// Query current governance stats.
72    pub fn stats(&self) -> MonitorStats {
73        MonitorStats {
74            active_obligations: self.obligations.active_count(),
75            violated_obligations: self.obligations.violated_count(),
76            active_ltl_monitors: self.temporal_monitors.len(),
77            current_time: self.current_time,
78        }
79    }
80}
81
82#[derive(Debug, Clone, Copy)]
83pub struct MonitorStats {
84    pub active_obligations: usize,
85    pub violated_obligations: usize,
86    pub active_ltl_monitors: usize,
87    pub current_time: u64,
88}