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