Skip to main content

oxios_kernel/kernel_handle/
infra_api.rs

1//! Infra API — Git, scheduler, cron, resources, events, system.
2
3use crate::config::OxiosConfig;
4use crate::cron::{CronJob, CronJobUpdate, CronScheduler};
5use crate::event_bus::{EventBus, KernelEvent};
6use crate::git_layer::{GitLayer, LogEntry};
7use crate::resource_monitor::{ResourceMonitor, ResourceSnapshot};
8use crate::scheduler::{AgentScheduler, ScheduledTask, SchedulerStats};
9use std::sync::Arc;
10use std::time::{Duration, Instant};
11
12/// Infrastructure system calls.
13pub struct InfraApi {
14    pub(crate) git_layer: Arc<GitLayer>,
15    pub(crate) scheduler: Arc<AgentScheduler>,
16    pub(crate) cron_scheduler: Arc<CronScheduler>,
17    pub(crate) resource_monitor: Arc<ResourceMonitor>,
18    pub(crate) event_bus: EventBus,
19    pub(crate) config: OxiosConfig,
20    pub(crate) start_time: Instant,
21    /// Hot-reloadable orchestrator config (evolution iterations, score threshold).
22    pub(crate) orchestrator_config: parking_lot::RwLock<crate::config::OrchestratorConfig>,
23}
24
25impl InfraApi {
26    /// Create a new InfraApi.
27    pub fn new(
28        git_layer: Arc<GitLayer>,
29        scheduler: Arc<AgentScheduler>,
30        cron_scheduler: Arc<CronScheduler>,
31        resource_monitor: Arc<ResourceMonitor>,
32        event_bus: EventBus,
33        config: OxiosConfig,
34        start_time: Instant,
35    ) -> Self {
36        Self {
37            git_layer,
38            scheduler,
39            cron_scheduler,
40            resource_monitor,
41            event_bus,
42            config,
43            start_time,
44            orchestrator_config: parking_lot::RwLock::new(
45                crate::config::OrchestratorConfig::default(),
46            ),
47        }
48    }
49    /// Get a reference to the GitLayer.
50    pub fn git(&self) -> &GitLayer {
51        &self.git_layer
52    }
53
54    /// Get commit log.
55    pub fn git_log(&self, max: usize) -> anyhow::Result<Vec<LogEntry>> {
56        self.git_layer.log(max)
57    }
58
59    /// Tag current state.
60    pub fn git_tag(&self, name: &str, message: &str) -> anyhow::Result<()> {
61        self.git_layer.tag(name, message)
62    }
63
64    /// Restore file from commit.
65    pub fn git_restore(&self, path: &str, hash: &str) -> anyhow::Result<()> {
66        self.git_layer.restore_file(path, hash)
67    }
68
69    /// Verify git repository integrity.
70    pub fn git_verify(&self) -> anyhow::Result<bool> {
71        self.git_layer.verify()
72    }
73
74    /// List git tags.
75    pub fn git_tags(&self) -> anyhow::Result<Vec<String>> {
76        self.git_layer.list_tags()
77    }
78
79    /// Get scheduler stats.
80    pub fn scheduler_stats(&self) -> SchedulerStats {
81        self.scheduler.stats()
82    }
83
84    /// Get queued tasks.
85    pub fn queued_tasks(&self) -> Vec<ScheduledTask> {
86        self.scheduler.queued_tasks()
87    }
88
89    /// Get running tasks.
90    pub fn running_tasks(&self) -> Vec<ScheduledTask> {
91        self.scheduler.running_tasks()
92    }
93
94    /// Add a cron job.
95    pub async fn add_cron(&self, job: CronJob) -> anyhow::Result<uuid::Uuid> {
96        self.cron_scheduler.add_job(job).await
97    }
98
99    /// Get a cron job by ID.
100    pub fn get_cron(&self, id: uuid::Uuid) -> Option<CronJob> {
101        self.cron_scheduler.get_job(id)
102    }
103
104    /// Update a cron job.
105    pub async fn update_cron(&self, id: uuid::Uuid, update: CronJobUpdate) -> anyhow::Result<()> {
106        self.cron_scheduler.update_job(id, update).await
107    }
108
109    /// Remove a cron job by ID.
110    pub async fn remove_cron(&self, id: uuid::Uuid) -> anyhow::Result<()> {
111        self.cron_scheduler.remove_job(id).await
112    }
113
114    /// Trigger a cron job manually.
115    pub fn trigger_cron(&self, id: uuid::Uuid) -> anyhow::Result<CronJob> {
116        self.cron_scheduler.trigger_job(id)
117    }
118
119    /// Mark cron job completed.
120    pub async fn complete_cron(&self, id: uuid::Uuid, success: bool, summary: String) {
121        self.cron_scheduler
122            .mark_job_completed(id, success, summary)
123            .await
124    }
125
126    /// List all cron jobs.
127    pub fn list_crons(&self) -> Vec<CronJob> {
128        self.cron_scheduler.list_jobs()
129    }
130
131    /// Get resource snapshot.
132    pub fn resource_snapshot(&self) -> ResourceSnapshot {
133        self.resource_monitor.snapshot()
134    }
135
136    /// Get resource history snapshots.
137    pub fn resource_history(&self, last_n: usize) -> Vec<ResourceSnapshot> {
138        self.resource_monitor.history(last_n)
139    }
140
141    /// Check if system is overloaded.
142    pub fn is_overloaded(&self) -> bool {
143        self.resource_monitor.is_overloaded()
144    }
145
146    /// Subscribe to kernel events.
147    pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<KernelEvent> {
148        self.event_bus.subscribe()
149    }
150
151    /// Publish a kernel event.
152    pub fn publish(&self, event: KernelEvent) -> anyhow::Result<()> {
153        self.event_bus
154            .publish(event)
155            .map_err(|e| anyhow::anyhow!("broadcast error: {e}"))
156    }
157
158    /// Get config reference.
159    pub fn config(&self) -> &OxiosConfig {
160        &self.config
161    }
162
163    /// Scheduler reference — for hot-reload config propagation.
164    pub fn scheduler(&self) -> &Arc<AgentScheduler> {
165        &self.scheduler
166    }
167
168    /// Resource monitor reference — for hot-reload config propagation.
169    pub fn resource_monitor(&self) -> &Arc<ResourceMonitor> {
170        &self.resource_monitor
171    }
172
173    /// Hot-reload orchestrator config (stored in InfraApi for propagation).
174    pub fn update_orchestrator_config(&self, config: crate::config::OrchestratorConfig) {
175        *self.orchestrator_config.write() = config;
176    }
177
178    /// Get system uptime.
179    pub fn uptime(&self) -> Duration {
180        self.start_time.elapsed()
181    }
182}