File size: 6,882 Bytes
1269259 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | // 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<String>,
pub assigned_thinkers: HashSet<String>,
pub tasks: Vec<ProjectTask>,
pub context: String,
pub results: Vec<String>,
}
/// 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<String>, // worker pub_key
pub status: String, // Pending, InProgress, Done, Failed
}
/// Registry of all projects.
pub struct ProjectRegistry {
pub projects: HashMap<String, Project>,
}
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<Project> {
self.projects.remove(id)
}
pub fn list_projects(&self) -> Vec<String> {
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<String>,
pub current_task: Option<String>,
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<String>,
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<String, WorkerState>,
pub thinkers: HashMap<String, ThinkerState>,
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(),
)
}
}
|