Skip to main content

oxios_kernel/kernel_handle/
infra_api.rs

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