terraphim_orchestrator 1.16.34

AI Dark Factory orchestrator wiring spawner, router, supervisor into a reconciliation loop
Documentation
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! Dual-mode orchestrator with full integration.
//!
//! Manages both time-driven and issue-driven agent execution modes
//! with shared concurrency control and unified status.

use crate::{
    AgentDefinition, AgentOrchestrator, CompoundReviewResult, ConcurrencyController,
    DispatcherStats, FairnessPolicy, HandoffContext, ModeQuotas, OrchestratorConfig, ScheduleEvent,
    TimeScheduler, WorkflowConfig,
};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use terraphim_tracker::{GiteaTracker, IssueTracker};
use tokio::sync::{mpsc, watch, Mutex};
use tracing::{error, info, warn};

/// Shared state between time and issue modes.
#[derive(Clone)]
pub struct SharedState {
    /// Concurrency controller for agent limits.
    pub concurrency: ConcurrencyController,
    /// Statistics from both modes.
    pub stats: Arc<Mutex<DualModeStats>>,
    /// Shutdown signal.
    pub shutdown_tx: watch::Sender<bool>,
}

/// Statistics for dual-mode operation.
#[derive(Debug, Default)]
pub struct DualModeStats {
    /// Time-driven statistics.
    pub time_stats: Option<DispatcherStats>,
    /// Issue-driven statistics.
    pub issue_stats: Option<DispatcherStats>,
    /// Total agents spawned.
    pub total_agents_spawned: u64,
    /// Active agents by mode.
    pub active_by_mode: HashMap<String, usize>,
}

/// Agent identifier with mode.
#[derive(Debug, Clone)]
pub struct AgentId {
    /// Agent name or issue identifier.
    pub name: String,
    /// Source mode.
    pub mode: ExecutionMode,
}

/// Execution mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExecutionMode {
    /// Time-driven (cron-based).
    TimeDriven,
    /// Issue-driven (tracker-based).
    IssueDriven,
}

impl std::fmt::Display for ExecutionMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ExecutionMode::TimeDriven => write!(f, "time"),
            ExecutionMode::IssueDriven => write!(f, "issue"),
        }
    }
}

/// Task spawned by either mode.
#[derive(Debug, Clone)]
pub enum SpawnTask {
    /// Time-driven agent task.
    TimeTask { agent: Box<AgentDefinition> },
    /// Issue-driven agent task.
    IssueTask { issue_id: String, title: String },
}

/// Full dual-mode orchestrator.
pub struct DualModeOrchestrator {
    /// Configuration.
    config: OrchestratorConfig,
    /// Base orchestrator for time-driven mode.
    base: AgentOrchestrator,
    /// Shared state.
    state: SharedState,
    /// Time mode controller (if enabled).
    time_mode: Option<TimeModeComponents>,
    /// Issue mode controller (if enabled).
    issue_mode: Option<IssueModeComponents>,
    /// Task receiver from both modes.
    task_rx: mpsc::Receiver<SpawnTask>,
    /// Task sender for dispatching from modes.
    task_tx: mpsc::Sender<SpawnTask>,
    /// Active agents.
    active_agents: Arc<Mutex<HashMap<String, AgentId>>>,
}

/// Components for time mode.
struct TimeModeComponents {
    scheduler: TimeScheduler,
    shutdown_rx: watch::Receiver<bool>,
}

/// Components for issue mode.
struct IssueModeComponents {
    tracker: Box<dyn IssueTracker>,
    workflow: WorkflowConfig,
    shutdown_rx: watch::Receiver<bool>,
}

impl DualModeOrchestrator {
    /// Create a new dual-mode orchestrator.
    pub fn new(config: OrchestratorConfig) -> Result<Self, crate::OrchestratorError> {
        let base = AgentOrchestrator::new(config.clone())?;

        // Create concurrency controller
        let concurrency = if let Some(ref workflow) = config.workflow {
            ConcurrencyController::new(
                workflow.concurrency.global_max,
                ModeQuotas {
                    time_max: workflow
                        .concurrency
                        .global_max
                        .saturating_sub(workflow.concurrency.issue_max),
                    issue_max: workflow.concurrency.issue_max,
                },
                workflow
                    .concurrency
                    .fairness
                    .parse()
                    .unwrap_or(FairnessPolicy::RoundRobin),
            )
        } else {
            ConcurrencyController::new(10, ModeQuotas::default(), FairnessPolicy::RoundRobin)
        };

        // Create shared state
        let (shutdown_tx, _shutdown_rx) = watch::channel(false);
        let state = SharedState {
            concurrency,
            stats: Arc::new(Mutex::new(DualModeStats::default())),
            shutdown_tx,
        };

        // Create task channel for modes to send spawned tasks to orchestrator
        let (task_tx, task_rx) = mpsc::channel(128);

        // Setup time mode
        let time_mode = {
            let scheduler =
                TimeScheduler::new(&config.agents, Some(&config.compound_review.schedule))?;
            let shutdown_rx = state.shutdown_tx.subscribe();
            Some(TimeModeComponents {
                scheduler,
                shutdown_rx,
            })
        };

        // Setup issue mode if configured
        let issue_mode = if let Some(ref workflow) = config.workflow {
            if workflow.enabled {
                match create_tracker(workflow) {
                    Ok(tracker) => {
                        let shutdown_rx = state.shutdown_tx.subscribe();
                        Some(IssueModeComponents {
                            tracker,
                            workflow: workflow.clone(),
                            shutdown_rx,
                        })
                    }
                    Err(e) => {
                        warn!("failed to create issue tracker: {}", e);
                        None
                    }
                }
            } else {
                None
            }
        } else {
            None
        };

        Ok(Self {
            config,
            base,
            state,
            time_mode,
            issue_mode,
            task_rx,
            task_tx,
            active_agents: Arc::new(Mutex::new(HashMap::new())),
        })
    }

    /// Access the orchestrator configuration.
    pub fn config(&self) -> &OrchestratorConfig {
        &self.config
    }

    /// Run the dual-mode orchestrator.
    pub async fn run(&mut self) -> Result<(), crate::OrchestratorError> {
        info!(
            agents = self.config.agents.len(),
            workflow_enabled = self.config.workflow.as_ref().is_some_and(|w| w.enabled),
            "starting dual-mode orchestrator"
        );

        // Start time mode task
        let mut time_handle = if let Some(time_components) = self.time_mode.take() {
            let state = self.state.clone();
            Some(tokio::spawn(run_time_mode(time_components, state)))
        } else {
            None
        };

        // Start issue mode task
        let mut issue_handle = if let Some(issue_components) = self.issue_mode.take() {
            let state = self.state.clone();
            Some(tokio::spawn(run_issue_mode(issue_components, state)))
        } else {
            None
        };

        // Wait for shutdown signal, task completion, or spawned tasks
        let ctrl_c = tokio::signal::ctrl_c();
        tokio::pin!(ctrl_c);

        // Pin the optional handles for select
        let mut time_done = false;
        let mut issue_done = false;

        loop {
            tokio::select! {
                // Receive spawned tasks from modes and track them
                Some(task) = self.task_rx.recv() => {
                    self.track_spawned_task(task).await;
                }
                // Wait for time mode completion
                result = async {
                    match &mut time_handle {
                        Some(h) => h.await,
                        None => std::future::pending().await,
                    }
                }, if !time_done => {
                    time_done = true;
                    match result {
                        Ok(()) => info!("time mode completed"),
                        Err(e) => error!("time mode panicked: {}", e),
                    }
                    if issue_done { break; }
                }
                // Wait for issue mode completion
                result = async {
                    match &mut issue_handle {
                        Some(h) => h.await,
                        None => std::future::pending().await,
                    }
                }, if !issue_done => {
                    issue_done = true;
                    match result {
                        Ok(()) => info!("issue mode completed"),
                        Err(e) => error!("issue mode panicked: {}", e),
                    }
                    if time_done { break; }
                }
                // Base orchestrator reconciliation loop
                result = self.base.run() => {
                    match result {
                        Ok(()) => info!("base orchestrator completed"),
                        Err(e) => error!("base orchestrator error: {}", e),
                    }
                    break;
                }
                _ = &mut ctrl_c => {
                    info!("shutdown signal received");
                    let _ = self.state.shutdown_tx.send(true);
                    break;
                }
            }
        }

        // Graceful shutdown
        info!("shutting down dual-mode orchestrator");
        self.shutdown().await;

        Ok(())
    }

    /// Track a spawned task from either mode.
    async fn track_spawned_task(&self, task: SpawnTask) {
        let mut stats = self.state.stats.lock().await;
        stats.total_agents_spawned += 1;
        match &task {
            SpawnTask::TimeTask { agent } => {
                info!(agent_name = %agent.name, "received time-driven spawn task");
                let mut agents = self.active_agents.lock().await;
                agents.insert(
                    agent.name.clone(),
                    AgentId {
                        name: agent.name.clone(),
                        mode: ExecutionMode::TimeDriven,
                    },
                );
                *stats.active_by_mode.entry("time".into()).or_insert(0) += 1;
            }
            SpawnTask::IssueTask { issue_id, title } => {
                info!(issue_id = %issue_id, title = %title, "received issue-driven spawn task");
                let mut agents = self.active_agents.lock().await;
                agents.insert(
                    issue_id.clone(),
                    AgentId {
                        name: issue_id.clone(),
                        mode: ExecutionMode::IssueDriven,
                    },
                );
                *stats.active_by_mode.entry("issue".into()).or_insert(0) += 1;
            }
        }
    }

    /// Get a clone of the task sender for external dispatch.
    pub fn task_sender(&self) -> mpsc::Sender<SpawnTask> {
        self.task_tx.clone()
    }

    /// Request shutdown.
    pub fn request_shutdown(&self) {
        let _ = self.state.shutdown_tx.send(true);
    }

    /// Shutdown gracefully.
    async fn shutdown(&mut self) {
        info!("initiating graceful shutdown");

        // Stop accepting new tasks
        self.request_shutdown();

        // Wait for active agents to complete
        let timeout = Duration::from_secs(30);
        let start = std::time::Instant::now();

        loop {
            let active_count = {
                let agents = self.active_agents.lock().await;
                agents.len()
            };

            if active_count == 0 {
                info!("all agents completed");
                break;
            }

            if start.elapsed() > timeout {
                warn!(
                    "shutdown timeout reached with {} agents still active",
                    active_count
                );
                break;
            }

            tokio::time::sleep(Duration::from_millis(100)).await;
        }

        // Shutdown base orchestrator
        self.base.shutdown();

        info!("shutdown complete");
    }

    /// Get current statistics.
    pub async fn stats(&self) -> DualModeStats {
        let stats = self.state.stats.lock().await;
        stats.clone()
    }

    /// Get active agent count.
    pub async fn active_count(&self) -> usize {
        let agents = self.active_agents.lock().await;
        agents.len()
    }

    /// Trigger compound review.
    pub async fn trigger_compound_review(
        &mut self,
        git_ref: &str,
        base_ref: &str,
    ) -> Result<CompoundReviewResult, crate::OrchestratorError> {
        self.base.trigger_compound_review(git_ref, base_ref).await
    }

    /// Handoff task between agents.
    pub async fn handoff(
        &mut self,
        from_agent: &str,
        to_agent: &str,
        ctx: HandoffContext,
    ) -> Result<(), crate::OrchestratorError> {
        self.base.handoff(from_agent, to_agent, ctx).await
    }
}

/// Run time mode in background.
async fn run_time_mode(components: TimeModeComponents, state: SharedState) {
    info!("starting time mode task");

    let TimeModeComponents {
        mut scheduler,
        mut shutdown_rx,
    } = components;

    // Get immediate agents (Safety layer)
    let immediate = scheduler.immediate_agents();
    for agent in immediate {
        info!(agent_name = %agent.name, "spawning immediate Safety agent");
        // Safety agents spawn without concurrency limit
    }

    loop {
        tokio::select! {
            event = scheduler.next_event() => {
                match event {
                    ScheduleEvent::Spawn(agent) => {
                        // Try to acquire time-driven slot
                        match state.concurrency.acquire_time_driven().await {
                            Some(permit) => {
                                info!(agent_name = %agent.name, "spawning time-driven agent");
                                // Spawn agent here
                                drop(permit);
                            }
                            None => {
                                warn!(agent_name = %agent.name, "no slot available for time-driven agent");
                            }
                        }
                    }
                    ScheduleEvent::Stop { agent_name } => {
                        info!(agent_name = %agent_name, "stopping agent");
                    }
                    ScheduleEvent::CompoundReview => {
                        info!("compound review triggered");
                    }
                    ScheduleEvent::Flow(flow) => {
                        info!(flow_name = %flow.name, "flow triggered");
                    }
                }
            }
            _ = shutdown_rx.changed() => {
                if *shutdown_rx.borrow() {
                    info!("time mode shutting down");
                    break;
                }
            }
        }
    }
}

/// Run issue mode in background.
async fn run_issue_mode(components: IssueModeComponents, state: SharedState) {
    info!("starting issue mode task");

    let IssueModeComponents {
        tracker,
        workflow,
        mut shutdown_rx,
    } = components;

    let poll_interval = Duration::from_secs(workflow.poll_interval_secs);

    loop {
        tokio::select! {
            _ = tokio::time::sleep(poll_interval) => {
                match tracker.fetch_candidate_issues().await {
                    Ok(issues) => {
                        info!(count = issues.len(), "fetched candidate issues");

                        for issue in issues {
                            // Skip blocked issues
                            if !issue.all_blockers_terminal(&workflow.tracker.states.terminal) {
                                continue;
                            }

                            // Try to acquire issue-driven slot
                            match state.concurrency.acquire_issue_driven().await {
                                Some(permit) => {
                                    info!(
                                        issue_id = %issue.id,
                                        title = %issue.title,
                                        "dispatching issue-driven agent"
                                    );
                                    // Spawn agent here
                                    drop(permit);
                                }
                                None => {
                                    warn!("no slot available for issue-driven agent");
                                    break; // Stop trying until slots free up
                                }
                            }
                        }
                    }
                    Err(e) => {
                        error!("failed to fetch issues: {}", e);
                    }
                }
            }
            _ = shutdown_rx.changed() => {
                if *shutdown_rx.borrow() {
                    info!("issue mode shutting down");
                    break;
                }
            }
        }
    }
}

/// Create tracker from workflow config.
fn create_tracker(workflow: &WorkflowConfig) -> Result<Box<dyn IssueTracker>, String> {
    match workflow.tracker.kind.as_str() {
        "gitea" => {
            use terraphim_tracker::gitea::GiteaConfig;
            let tracker = GiteaTracker::new(GiteaConfig {
                base_url: workflow.tracker.endpoint.clone(),
                token: workflow.tracker.api_key.clone(),
                owner: workflow.tracker.owner.clone(),
                repo: workflow.tracker.repo.clone(),
                active_states: workflow.tracker.states.active.clone(),
                terminal_states: workflow.tracker.states.terminal.clone(),
                use_robot_api: workflow.tracker.use_robot_api,
                robot_path: std::path::PathBuf::from("/home/alex/go/bin/gitea-robot"),
                claim_strategy: terraphim_tracker::gitea::ClaimStrategy::PreferRobot,
            })
            .map_err(|e| format!("failed to create Gitea tracker: {}", e))?;

            Ok(Box::new(tracker))
        }
        "linear" => {
            use terraphim_tracker::{LinearConfig, LinearTracker};
            let project_slug = workflow
                .tracker
                .project_slug
                .clone()
                .ok_or("project_slug required for linear tracker")?;
            let tracker = LinearTracker::new(LinearConfig {
                endpoint: workflow.tracker.endpoint.clone(),
                api_key: workflow.tracker.api_key.clone(),
                project_slug,
                active_states: workflow.tracker.states.active.clone(),
                terminal_states: workflow.tracker.states.terminal.clone(),
            })
            .map_err(|e| format!("failed to create Linear tracker: {}", e))?;

            Ok(Box::new(tracker))
        }
        _ => Err(format!(
            "unsupported tracker kind: {}",
            workflow.tracker.kind
        )),
    }
}

impl Clone for DualModeStats {
    fn clone(&self) -> Self {
        Self {
            time_stats: self.time_stats.clone(),
            issue_stats: self.issue_stats.clone(),
            total_agents_spawned: self.total_agents_spawned,
            active_by_mode: self.active_by_mode.clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_execution_mode_display() {
        assert_eq!(ExecutionMode::TimeDriven.to_string(), "time");
        assert_eq!(ExecutionMode::IssueDriven.to_string(), "issue");
    }

    #[test]
    fn test_dual_mode_stats_default() {
        let stats = DualModeStats::default();
        assert_eq!(stats.total_agents_spawned, 0);
        assert!(stats.time_stats.is_none());
        assert!(stats.issue_stats.is_none());
    }
}