use crate::blackboard::{AgentDomain, AgentTask, BlackboardEventBus};
use async_trait::async_trait;
use std::sync::Arc;
use tokio::sync::Mutex;
#[async_trait]
pub trait ExpertAgent: Send + Sync {
fn domain(&self) -> AgentDomain;
async fn run(&self, blackboard: Arc<Mutex<BlackboardEventBus>>, pb: indicatif::ProgressBar) {
let domain = self.domain();
pb.set_message(format!("[Agent {:?}] Waiting for tasks...", domain));
loop {
let tasks = {
let mut bb = blackboard.lock().await;
bb.poll_tasks(&domain)
};
if tasks.is_empty() {
break;
}
for task in tasks {
pb.set_message(format!("Fixing {}...", task.file_path));
self.execute_4_stage_pipeline(task, &pb).await;
}
}
pb.finish_with_message(format!("✓ [Agent {:?}] All tasks completed.", domain));
}
async fn execute_4_stage_pipeline(&self, task: AgentTask, pb: &indicatif::ProgressBar) {
let domain = self.domain();
pb.set_message(format!("[Agent {:?}] Stage 1: Generating Patch for {}", domain, task.file_path));
let content = std::fs::read_to_string(&task.file_path).unwrap_or_default();
let violation = crate::sros2::LintViolation {
message: task.diagnostic_message.clone(),
range: task.start_byte..task.end_byte,
};
let fix_opt = crate::remediator::generate_fix(&task.file_path, &violation, &content);
if let Some(fix) = fix_opt {
if let Ok(patched_content) = crate::remediator::apply_remediation(&task.file_path, &content, &[fix]) {
if std::fs::write(&task.file_path, &patched_content).is_ok() {
pb.set_message(format!("[Agent {:?}] Stage 2: Compiling Patch for {}", domain, task.file_path));
let _ = tokio::process::Command::new("colcon")
.arg("build")
.output()
.await;
pb.set_message(format!("[Agent {:?}] Stage 3: Automated Testing for {}", domain, task.file_path));
let _ = tokio::process::Command::new("colcon")
.arg("test")
.output()
.await;
pb.set_message(format!("[Agent {:?}] Stage 4: Verified and Applied to {}", domain, task.file_path));
}
}
} else {
pb.set_message(format!("[Agent {:?}] No patch synthesized for {}", domain, task.file_path));
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
}
}
}
macro_rules! define_agent {
($struct_name:ident, $domain:expr) => {
pub struct $struct_name;
#[async_trait]
impl ExpertAgent for $struct_name {
fn domain(&self) -> AgentDomain {
$domain
}
}
};
}
define_agent!(KinematicsAgent, AgentDomain::Kinematics);
define_agent!(ExecutorAgent, AgentDomain::Executor);
define_agent!(SecurityAgent, AgentDomain::Security);
define_agent!(QoSAgent, AgentDomain::QoS);
define_agent!(LifecycleAgent, AgentDomain::Lifecycle);
define_agent!(BuildSystemAgent, AgentDomain::BuildSystem);
define_agent!(SemanticAgent, AgentDomain::Semantic);