terraphim_orchestrator 1.20.1

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
//! Global concurrency controller with fairness.
//!
//! Enforces global agent limits, per-mode quotas, and per-project caps, and
//! ensures fairness between time-driven and issue-driven modes.

use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::{Mutex, Semaphore};

/// Concurrency controller with fairness policies.
#[derive(Clone)]
pub struct ConcurrencyController {
    /// Global semaphore for total agent limit.
    global: Arc<Semaphore>,
    /// Per-mode quotas.
    quotas: ModeQuotas,
    /// Optional per-project caps (project id -> max concurrent agents).
    /// Missing entries mean "no per-project cap".
    project_caps: Arc<HashMap<String, ProjectCaps>>,
    /// Currently running agents by mode + per-project.
    running: Arc<Mutex<RunningCounts>>,
    /// Fairness policy.
    fairness: FairnessPolicy,
}

/// Quotas for each mode.
#[derive(Debug, Clone, Copy)]
pub struct ModeQuotas {
    /// Maximum concurrent time-driven agents.
    pub time_max: usize,
    /// Maximum concurrent issue-driven agents.
    pub issue_max: usize,
}

/// Per-project concurrency caps.
#[derive(Debug, Clone, Copy)]
pub struct ProjectCaps {
    /// Maximum concurrent agents (time + issue + mention) for this project.
    pub max_concurrent_agents: usize,
    /// Maximum concurrent mention-driven agents for this project.
    /// None means "no per-project mention cap" (fall back to global).
    pub max_concurrent_mention_agents: Option<usize>,
}

/// Currently running agent counts.
#[derive(Debug, Default)]
struct RunningCounts {
    time_driven: usize,
    issue_driven: usize,
    mention_driven: usize,
    per_project: HashMap<String, ProjectRunning>,
}

#[derive(Debug, Default, Clone, Copy)]
struct ProjectRunning {
    total: usize,
    mention: usize,
}

/// Fairness policy for mode coordination.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FairnessPolicy {
    /// Round-robin: alternate between modes when both have work.
    RoundRobin,
    /// Priority: always prefer higher priority tasks regardless of mode.
    Priority,
    /// Proportional: allocate slots proportionally to mode quotas.
    Proportional,
}

impl std::str::FromStr for FairnessPolicy {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "round_robin" | "round-robin" | "roundrobin" => Ok(FairnessPolicy::RoundRobin),
            "priority" => Ok(FairnessPolicy::Priority),
            "proportional" => Ok(FairnessPolicy::Proportional),
            _ => Err(format!("unknown fairness policy: {}", s)),
        }
    }
}

/// Permit for a running agent. Releases the slot when dropped.
pub struct AgentPermit {
    _global: tokio::sync::OwnedSemaphorePermit,
    mode: AgentMode,
    project: String,
    running: Arc<Mutex<RunningCounts>>,
}

#[derive(Debug, Clone, Copy)]
enum AgentMode {
    TimeDriven,
    IssueDriven,
    MentionDriven,
}

impl Drop for AgentPermit {
    fn drop(&mut self) {
        let mode = self.mode;
        let project = std::mem::take(&mut self.project);
        let running = self.running.clone();
        tokio::spawn(async move {
            let mut counts = running.lock().await;
            match mode {
                AgentMode::TimeDriven => counts.time_driven = counts.time_driven.saturating_sub(1),
                AgentMode::IssueDriven => {
                    counts.issue_driven = counts.issue_driven.saturating_sub(1)
                }
                AgentMode::MentionDriven => {
                    counts.mention_driven = counts.mention_driven.saturating_sub(1)
                }
            }
            if let Some(proj) = counts.per_project.get_mut(&project) {
                proj.total = proj.total.saturating_sub(1);
                if let AgentMode::MentionDriven = mode {
                    proj.mention = proj.mention.saturating_sub(1);
                }
                if proj.total == 0 && proj.mention == 0 {
                    counts.per_project.remove(&project);
                }
            }
        });
    }
}

impl ConcurrencyController {
    /// Create a new concurrency controller with no per-project caps.
    pub fn new(global_max: usize, quotas: ModeQuotas, fairness: FairnessPolicy) -> Self {
        Self::with_project_caps(global_max, quotas, fairness, HashMap::new())
    }

    /// Create a new concurrency controller with explicit per-project caps.
    pub fn with_project_caps(
        global_max: usize,
        quotas: ModeQuotas,
        fairness: FairnessPolicy,
        project_caps: HashMap<String, ProjectCaps>,
    ) -> Self {
        Self {
            global: Arc::new(Semaphore::new(global_max)),
            quotas,
            project_caps: Arc::new(project_caps),
            running: Arc::new(Mutex::new(RunningCounts::default())),
            fairness,
        }
    }

    /// Try to acquire a slot for a time-driven agent in the given project.
    pub async fn acquire_time_driven(&self, project: &str) -> Option<AgentPermit> {
        self.acquire(AgentMode::TimeDriven, project).await
    }

    /// Try to acquire a slot for an issue-driven agent in the given project.
    pub async fn acquire_issue_driven(&self, project: &str) -> Option<AgentPermit> {
        self.acquire(AgentMode::IssueDriven, project).await
    }

    /// Try to acquire a slot for a mention-driven agent in the given project.
    pub async fn acquire_mention_driven(&self, project: &str) -> Option<AgentPermit> {
        self.acquire(AgentMode::MentionDriven, project).await
    }

    /// Try to acquire a generic slot without mode-specific quota checks.
    ///
    /// Only enforces the global semaphore and per-project caps. Used when the
    /// caller does not know the agent mode (e.g. `spawn_agent` is shared across
    /// cron, mention, and issue-driven paths).
    pub async fn acquire_any(&self, project: &str) -> Option<AgentPermit> {
        if !self
            .project_has_capacity(AgentMode::TimeDriven, project)
            .await
        {
            return None;
        }
        let global_permit = match self.global.clone().try_acquire_owned() {
            Ok(p) => p,
            Err(_) => {
                tracing::debug!("global concurrency limit reached");
                return None;
            }
        };
        {
            let mut counts = self.running.lock().await;
            counts.time_driven += 1;
            let entry = counts.per_project.entry(project.to_string()).or_default();
            entry.total += 1;
        }
        tracing::debug!(project, "acquired generic concurrency slot");
        Some(AgentPermit {
            _global: global_permit,
            mode: AgentMode::TimeDriven,
            project: project.to_string(),
            running: self.running.clone(),
        })
    }

    /// Get current running counts (time_driven, issue_driven).
    pub async fn running_counts(&self) -> (usize, usize) {
        let counts = self.running.lock().await;
        (counts.time_driven, counts.issue_driven)
    }

    /// Get the running count for a specific project.
    pub async fn project_running_count(&self, project: &str) -> usize {
        let counts = self.running.lock().await;
        counts
            .per_project
            .get(project)
            .map(|p| p.total)
            .unwrap_or(0)
    }

    /// Get available slots.
    pub fn available_slots(&self) -> usize {
        self.global.available_permits()
    }

    /// Check if mode has capacity.
    async fn mode_has_capacity(&self, mode: AgentMode) -> bool {
        let counts = self.running.lock().await;
        match mode {
            AgentMode::TimeDriven => counts.time_driven < self.quotas.time_max,
            AgentMode::IssueDriven => counts.issue_driven < self.quotas.issue_max,
            // Mention-driven currently has no global mode quota; per-project
            // mention cap is checked separately in `project_has_capacity`.
            AgentMode::MentionDriven => true,
        }
    }

    /// Check if project has capacity for this mode.
    async fn project_has_capacity(&self, mode: AgentMode, project: &str) -> bool {
        let Some(caps) = self.project_caps.get(project) else {
            return true;
        };
        let counts = self.running.lock().await;
        let running = counts.per_project.get(project).copied().unwrap_or_default();

        if running.total >= caps.max_concurrent_agents {
            tracing::debug!(
                project,
                total = running.total,
                cap = caps.max_concurrent_agents,
                "per-project cap reached"
            );
            return false;
        }
        if matches!(mode, AgentMode::MentionDriven) {
            if let Some(mention_cap) = caps.max_concurrent_mention_agents {
                if running.mention >= mention_cap {
                    tracing::debug!(
                        project,
                        mention = running.mention,
                        cap = mention_cap,
                        "per-project mention cap reached"
                    );
                    return false;
                }
            }
        }
        true
    }

    /// Get the active fairness policy.
    pub fn fairness_policy(&self) -> FairnessPolicy {
        self.fairness
    }

    /// Acquire a slot for the given mode in the given project.
    async fn acquire(&self, mode: AgentMode, project: &str) -> Option<AgentPermit> {
        // Check mode quota first
        if !self.mode_has_capacity(mode).await {
            tracing::debug!(?mode, "mode quota exceeded");
            return None;
        }

        // Check per-project cap
        if !self.project_has_capacity(mode, project).await {
            return None;
        }

        // Apply fairness policy: under Proportional, check whether the mode
        // is consuming more than its fair share of global capacity.
        if self.fairness == FairnessPolicy::Proportional {
            let counts = self.running.lock().await;
            let total = counts.time_driven + counts.issue_driven;
            let global_cap = self.global.available_permits() + total;
            if global_cap > 0 {
                // Proportional fairness only applies to time/issue modes with
                // quotas; mention-driven has no mode quota and is exempt.
                let mode_count = match mode {
                    AgentMode::TimeDriven => counts.time_driven,
                    AgentMode::IssueDriven => counts.issue_driven,
                    AgentMode::MentionDriven => 0,
                };
                let mode_quota = match mode {
                    AgentMode::TimeDriven => self.quotas.time_max,
                    AgentMode::IssueDriven => self.quotas.issue_max,
                    AgentMode::MentionDriven => usize::MAX,
                };
                let total_quota = self.quotas.time_max + self.quotas.issue_max;
                // Fair share = global_cap * (mode_quota / total_quota)
                if !matches!(mode, AgentMode::MentionDriven) {
                    let fair_share = (global_cap * mode_quota) / total_quota.max(1);
                    if mode_count >= fair_share && fair_share > 0 {
                        tracing::debug!(
                            ?mode,
                            mode_count,
                            fair_share,
                            "proportional fairness limit"
                        );
                        return None;
                    }
                }
            }
        }

        // Try to acquire global permit
        let global_permit = match self.global.clone().try_acquire_owned() {
            Ok(p) => p,
            Err(_) => {
                tracing::debug!("global concurrency limit reached");
                return None;
            }
        };

        // Update running counts
        {
            let mut counts = self.running.lock().await;
            match mode {
                AgentMode::TimeDriven => counts.time_driven += 1,
                AgentMode::IssueDriven => counts.issue_driven += 1,
                AgentMode::MentionDriven => counts.mention_driven += 1,
            }
            let entry = counts.per_project.entry(project.to_string()).or_default();
            entry.total += 1;
            if matches!(mode, AgentMode::MentionDriven) {
                entry.mention += 1;
            }
        }

        tracing::debug!(?mode, project, "acquired concurrency slot");

        Some(AgentPermit {
            _global: global_permit,
            mode,
            project: project.to_string(),
            running: self.running.clone(),
        })
    }
}

impl Default for ModeQuotas {
    fn default() -> Self {
        Self {
            time_max: 3,
            issue_max: 2,
        }
    }
}

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

    const TEST_PROJECT: &str = "__global__";

    #[tokio::test]
    async fn test_acquire_release() {
        let controller = ConcurrencyController::new(
            2,
            ModeQuotas {
                time_max: 2,
                issue_max: 2,
            },
            FairnessPolicy::RoundRobin,
        );

        // Acquire first permit
        let permit1 = controller.acquire_time_driven(TEST_PROJECT).await;
        assert!(permit1.is_some());

        // Acquire second permit
        let permit2 = controller.acquire_time_driven(TEST_PROJECT).await;
        assert!(permit2.is_some());

        // Third should fail (global limit)
        let permit3 = controller.acquire_time_driven(TEST_PROJECT).await;
        assert!(permit3.is_none());

        // Drop one and try again
        drop(permit1);

        // Wait a bit for the drop to propagate
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;

        let permit4 = controller.acquire_time_driven(TEST_PROJECT).await;
        assert!(permit4.is_some());
    }

    #[tokio::test]
    async fn test_mode_quotas() {
        let controller = ConcurrencyController::new(
            10,
            ModeQuotas {
                time_max: 1,
                issue_max: 1,
            },
            FairnessPolicy::RoundRobin,
        );

        // Acquire time-driven slot
        let time_permit = controller.acquire_time_driven(TEST_PROJECT).await;
        assert!(time_permit.is_some());

        // Second time-driven should fail
        let time_permit2 = controller.acquire_time_driven(TEST_PROJECT).await;
        assert!(time_permit2.is_none());

        // But issue-driven should succeed
        let issue_permit = controller.acquire_issue_driven(TEST_PROJECT).await;
        assert!(issue_permit.is_some());

        // Second issue-driven should fail
        let issue_permit2 = controller.acquire_issue_driven(TEST_PROJECT).await;
        assert!(issue_permit2.is_none());
    }

    #[tokio::test]
    async fn test_running_counts() {
        let controller = ConcurrencyController::new(
            5,
            ModeQuotas {
                time_max: 3,
                issue_max: 3,
            },
            FairnessPolicy::RoundRobin,
        );

        let _time_permit = controller.acquire_time_driven(TEST_PROJECT).await.unwrap();
        let _issue_permit = controller.acquire_issue_driven(TEST_PROJECT).await.unwrap();

        let (time_count, issue_count) = controller.running_counts().await;
        assert_eq!(time_count, 1);
        assert_eq!(issue_count, 1);
    }

    #[tokio::test]
    async fn test_per_project_cap_saturates_independently() {
        let mut caps = HashMap::new();
        caps.insert(
            "alpha".to_string(),
            ProjectCaps {
                max_concurrent_agents: 1,
                max_concurrent_mention_agents: None,
            },
        );
        caps.insert(
            "beta".to_string(),
            ProjectCaps {
                max_concurrent_agents: 2,
                max_concurrent_mention_agents: None,
            },
        );
        let controller = ConcurrencyController::with_project_caps(
            10,
            ModeQuotas {
                time_max: 5,
                issue_max: 5,
            },
            FairnessPolicy::RoundRobin,
            caps,
        );

        // alpha: cap 1
        let a1 = controller.acquire_time_driven("alpha").await;
        assert!(a1.is_some());
        let a2 = controller.acquire_time_driven("alpha").await;
        assert!(
            a2.is_none(),
            "alpha should be saturated after reaching its cap of 1"
        );

        // beta: cap 2 — independent of alpha
        let b1 = controller.acquire_time_driven("beta").await;
        let b2 = controller.acquire_time_driven("beta").await;
        assert!(b1.is_some());
        assert!(b2.is_some());
        let b3 = controller.acquire_time_driven("beta").await;
        assert!(
            b3.is_none(),
            "beta should be saturated after reaching its cap of 2"
        );

        // Release alpha — permit slot returns to alpha independently.
        drop(a1);
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        let a3 = controller.acquire_time_driven("alpha").await;
        assert!(a3.is_some(), "alpha should have capacity after drop");
    }

    #[tokio::test]
    async fn test_per_project_mention_cap() {
        let mut caps = HashMap::new();
        caps.insert(
            "alpha".to_string(),
            ProjectCaps {
                max_concurrent_agents: 5,
                max_concurrent_mention_agents: Some(1),
            },
        );
        let controller = ConcurrencyController::with_project_caps(
            10,
            ModeQuotas {
                time_max: 5,
                issue_max: 5,
            },
            FairnessPolicy::RoundRobin,
            caps,
        );

        let m1 = controller.acquire_mention_driven("alpha").await;
        assert!(m1.is_some());
        let m2 = controller.acquire_mention_driven("alpha").await;
        assert!(
            m2.is_none(),
            "mention cap of 1 should block second mention acquire"
        );
        // Other modes still have capacity under the total cap of 5.
        let t1 = controller.acquire_time_driven("alpha").await;
        assert!(t1.is_some());
    }

    #[test]
    fn test_fairness_policy_from_str() {
        assert_eq!(
            "round_robin".parse::<FairnessPolicy>().unwrap(),
            FairnessPolicy::RoundRobin
        );
        assert_eq!(
            "priority".parse::<FairnessPolicy>().unwrap(),
            FairnessPolicy::Priority
        );
        assert_eq!(
            "proportional".parse::<FairnessPolicy>().unwrap(),
            FairnessPolicy::Proportional
        );
        assert!("unknown".parse::<FairnessPolicy>().is_err());
    }
}