// SPF Smart Gateway - Orchestrator State // Copyright 2026 Joseph Stone - All Rights Reserved // // P3: Project management, worker tracking, task assignment for mesh orchestrator nodes. // Additive only — does not break existing dispatch, mesh, or worker paths. // // Depends on: config.rs (AgentRole) use crate::config::AgentRole; use std::collections::{HashMap, HashSet}; use std::time::Instant; // ============================================================================ // PROJECT REGISTRY // ============================================================================ /// A single project managed by the orchestrator. pub struct Project { pub id: String, pub name: String, pub description: String, pub status: String, // Planning, Active, Review, Complete pub assigned_workers: HashSet, pub assigned_thinkers: HashSet, pub tasks: Vec, pub context: String, pub results: Vec, } /// A single task within a project. pub struct ProjectTask { pub id: String, pub title: String, pub tool: String, pub args: serde_json::Value, pub assigned_to: Option, // worker pub_key pub status: String, // Pending, InProgress, Done, Failed } /// Registry of all projects. pub struct ProjectRegistry { pub projects: HashMap, } impl ProjectRegistry { pub fn new() -> Self { Self { projects: HashMap::new() } } pub fn add_project(&mut self, project: Project) { self.projects.insert(project.id.clone(), project); } pub fn get_project(&self, id: &str) -> Option<&Project> { self.projects.get(id) } pub fn get_project_mut(&mut self, id: &str) -> Option<&mut Project> { self.projects.get_mut(id) } pub fn remove_project(&mut self, id: &str) -> Option { self.projects.remove(id) } pub fn list_projects(&self) -> Vec { self.projects.keys().cloned().collect() } } impl Default for ProjectRegistry { fn default() -> Self { Self::new() } } // ============================================================================ // WORKER / THINKER STATE // ============================================================================ /// State of a connected worker node. pub struct WorkerState { pub pub_key: String, pub status: String, // Idle, Busy, Offline pub current_project: Option, pub current_task: Option, pub last_heartbeat: Instant, } impl WorkerState { pub fn new(pub_key: String) -> Self { Self { pub_key, status: "Idle".to_string(), current_project: None, current_task: None, last_heartbeat: Instant::now(), } } } /// State of a connected thinker node. /// Same structure as WorkerState — separate type for clarity. pub struct ThinkerState { pub pub_key: String, pub status: String, pub current_project: Option, pub last_heartbeat: Instant, } impl ThinkerState { pub fn new(pub_key: String) -> Self { Self { pub_key, status: "Idle".to_string(), current_project: None, last_heartbeat: Instant::now(), } } } // ============================================================================ // ORCHESTRATOR STATE // ============================================================================ /// Top-level orchestrator state. /// Only instantiated on nodes with role == Orchestrator. pub struct OrchestratorState { pub my_role: AgentRole, pub my_pub_key: String, pub projects: ProjectRegistry, pub workers: HashMap, pub thinkers: HashMap, pub next_project_id: u64, } impl OrchestratorState { pub fn new(my_role: AgentRole, my_pub_key: String) -> Self { Self { my_role, my_pub_key, projects: ProjectRegistry::new(), workers: HashMap::new(), thinkers: HashMap::new(), next_project_id: 1, } } /// Generate next project ID. pub fn next_project_id(&mut self) -> String { let id = format!("proj-{}", self.next_project_id); self.next_project_id += 1; id } /// Record a worker heartbeat. pub fn heartbeat_worker(&mut self, pub_key: &str) { if let Some(worker) = self.workers.get_mut(pub_key) { worker.last_heartbeat = Instant::now(); if worker.status == "Offline" { worker.status = "Idle".to_string(); } } } /// Record a thinker heartbeat. pub fn heartbeat_thinker(&mut self, pub_key: &str) { if let Some(thinker) = self.thinkers.get_mut(pub_key) { thinker.last_heartbeat = Instant::now(); if thinker.status == "Offline" { thinker.status = "Idle".to_string(); } } } /// Assign a worker to a project. pub fn assign_worker(&mut self, pub_key: &str, project_id: &str) { self.workers .entry(pub_key.to_string()) .or_insert_with(|| WorkerState::new(pub_key.to_string())); if let Some(worker) = self.workers.get_mut(pub_key) { worker.status = "Idle".to_string(); worker.current_project = Some(project_id.to_string()); worker.current_task = None; } } /// Assign a thinker to a project. pub fn assign_thinker(&mut self, pub_key: &str, project_id: &str) { self.thinkers .entry(pub_key.to_string()) .or_insert_with(|| ThinkerState::new(pub_key.to_string())); if let Some(thinker) = self.thinkers.get_mut(pub_key) { thinker.status = "Idle".to_string(); thinker.current_project = Some(project_id.to_string()); } } /// Mark a worker as offline. pub fn worker_offline(&mut self, pub_key: &str) { if let Some(worker) = self.workers.get_mut(pub_key) { worker.status = "Offline".to_string(); } } /// Mark a thinker as offline. pub fn thinker_offline(&mut self, pub_key: &str) { if let Some(thinker) = self.thinkers.get_mut(pub_key) { thinker.status = "Offline".to_string(); } } /// Get summary of orchestrator state. pub fn summary(&self) -> String { format!( "Orchestrator | role={} | projects={} | workers={} idle / {} total | thinkers={} idle / {} total", self.my_role, self.projects.projects.len(), self.workers.values().filter(|w| w.status == "Idle").count(), self.workers.len(), self.thinkers.values().filter(|t| t.status == "Idle").count(), self.thinkers.len(), ) } }