Skip to main content

systemprompt_agent/services/agent_orchestration/
mod.rs

1//! Supervision of agent worker processes: lifecycle, monitoring, and
2//! reconciliation.
3//!
4//! This module groups the services that keep the database's view of running
5//! agents consistent with the OS process table. [`AgentOrchestrator`] is the
6//! top-level facade; the submodules cover process lifecycle, health
7//! monitoring, drift reconciliation, port allocation, the event bus, and the
8//! low-level process primitives. [`AgentStatus`] is the shared status model and
9//! [`OrchestrationError`] the unified error type.
10//!
11//! Copyright (c) systemprompt.io — Business Source License 1.1.
12//! See <https://systemprompt.io> for licensing details.
13
14pub mod database;
15pub mod event_bus;
16pub mod events;
17pub mod lifecycle;
18pub mod monitor;
19pub mod orchestrator;
20pub mod port_service;
21pub mod process;
22pub mod reconciler;
23
24use systemprompt_identifiers::AgentId;
25
26pub use event_bus::AgentEventBus;
27pub use events::AgentEvent;
28pub use orchestrator::{AgentInfo, AgentOrchestrator};
29pub use port_service::PortService;
30
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub enum AgentStatus {
33    Running {
34        pid: u32,
35        port: u16,
36    },
37    Failed {
38        reason: String,
39        last_attempt: Option<String>,
40        retry_count: u32,
41    },
42}
43
44#[derive(Debug, Clone)]
45pub struct AgentRuntimeConfig {
46    pub id: AgentId,
47    pub name: String,
48    pub port: u16,
49}
50
51#[derive(Debug, Clone)]
52pub struct ValidationReport {
53    pub valid: bool,
54    pub issues: Vec<String>,
55}
56
57impl Default for ValidationReport {
58    fn default() -> Self {
59        Self::new()
60    }
61}
62
63impl ValidationReport {
64    pub const fn new() -> Self {
65        Self {
66            valid: true,
67            issues: Vec::new(),
68        }
69    }
70
71    pub fn with_issue(issue: String) -> Self {
72        Self {
73            valid: false,
74            issues: vec![issue],
75        }
76    }
77
78    pub fn add_issue(&mut self, issue: String) {
79        self.valid = false;
80        self.issues.push(issue);
81    }
82}
83
84use crate::services::shared::AgentServiceError;
85use thiserror::Error;
86
87#[derive(Error, Debug)]
88pub enum OrchestrationError {
89    #[error("Agent {0} not found")]
90    AgentNotFound(String),
91
92    #[error("Agent {0} already running")]
93    AgentAlreadyRunning(String),
94
95    #[error("Process spawn failed: {0}")]
96    ProcessSpawnFailed(String),
97
98    #[error("Database error: {0}")]
99    DatabaseError(#[from] sqlx::Error),
100
101    #[error("Database error: {0}")]
102    Database(String),
103
104    #[error("IO error: {0}")]
105    IoError(#[from] std::io::Error),
106
107    #[error("Health check timeout for agent {0}")]
108    HealthCheckTimeout(String),
109
110    #[error("Generic error: {0}")]
111    Generic(String),
112
113    #[error("agent: {0}")]
114    Agent(#[from] crate::error::AgentError),
115
116    #[error("Service error: {0}")]
117    AgentService(#[from] AgentServiceError),
118}
119
120pub type OrchestrationResult<T> = Result<T, OrchestrationError>;