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