nusy-conductor 0.15.2

Agent orchestration conductor — reads kanban state via NATS, decomposes expeditions, tracks agent availability
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
588
589
590
591
592
593
594
595
596
597
598
599
//! Blocker detection + progress tracking + daily summary.
//!
//! Monitors the work graph for:
//! - Stale items (in_progress longer than threshold)
//! - Blocked items (unfinished dependencies)
//! - Resource conflicts (e.g., GPU queue contention)
//!
//! Generates human-readable daily summaries broadcast via NATS.

use crate::reader::{WorkGraph, WorkItem};
use crate::state::{AgentState, AssigneeTracker};
use std::collections::HashMap;
use std::time::Duration;

/// Default staleness threshold: 2 days.
const DEFAULT_STALE_THRESHOLD: Duration = Duration::from_secs(2 * 24 * 3600);

/// Configuration for the blocker monitor.
#[derive(Debug, Clone)]
pub struct MonitorConfig {
    /// How long an item can be in_progress before it's flagged as stale.
    pub stale_threshold: Duration,
}

impl Default for MonitorConfig {
    fn default() -> Self {
        MonitorConfig {
            stale_threshold: DEFAULT_STALE_THRESHOLD,
        }
    }
}

/// A flagged item in the monitoring report.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FlaggedItem {
    /// The item ID.
    pub id: String,
    /// The item title.
    pub title: String,
    /// Assignee, if any.
    pub assignee: Option<String>,
    /// Why this item was flagged.
    pub reason: FlagReason,
}

/// Why an item was flagged.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FlagReason {
    /// In progress longer than the threshold.
    Stale { status: String },
    /// Blocked by one or more incomplete dependencies.
    BlockedByDeps { blockers: Vec<String> },
    /// Resource conflict (e.g., GPU needed but occupied).
    ResourceConflict { resource: String, held_by: String },
}

impl std::fmt::Display for FlagReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Stale { status } => write!(f, "stale ({status})"),
            Self::BlockedByDeps { blockers } => {
                write!(f, "blocked by: {}", blockers.join(", "))
            }
            Self::ResourceConflict { resource, held_by } => {
                write!(f, "{resource} conflict (held by {held_by})")
            }
        }
    }
}

/// Daily summary report.
#[derive(Debug, Clone)]
pub struct DailySummary {
    /// Items completed (status = done) — for the summary period.
    pub completed: Vec<SummaryItem>,
    /// Items currently in progress.
    pub in_progress: Vec<SummaryItem>,
    /// Flagged items (stale, blocked, conflicts).
    pub flagged: Vec<FlaggedItem>,
    /// Agent availability summary.
    pub agent_states: Vec<AgentState>,
    /// Status counts across the board.
    pub status_counts: HashMap<String, usize>,
}

/// A summarized item for the daily report.
#[derive(Debug, Clone)]
pub struct SummaryItem {
    pub id: String,
    pub title: String,
    pub assignee: Option<String>,
    pub status: String,
}

impl From<&WorkItem> for SummaryItem {
    fn from(item: &WorkItem) -> Self {
        SummaryItem {
            id: item.id.clone(),
            title: item.title.clone(),
            assignee: item.assignee.clone(),
            status: item.status.clone(),
        }
    }
}

/// The blocker monitor — analyzes the work graph for issues.
pub struct BlockerMonitor {
    /// TODO: Use stale_threshold when WorkItem gains timestamps.
    #[allow(dead_code)]
    config: MonitorConfig,
    tracker: AssigneeTracker,
}

impl BlockerMonitor {
    /// Create with default config and fleet profiles.
    pub fn new() -> Self {
        BlockerMonitor {
            config: MonitorConfig::default(),
            tracker: AssigneeTracker::with_defaults(),
        }
    }

    /// Create with custom config.
    pub fn with_config(config: MonitorConfig) -> Self {
        BlockerMonitor {
            config,
            tracker: AssigneeTracker::with_defaults(),
        }
    }

    /// Detect all flagged items in the work graph.
    pub fn detect_issues(&self, graph: &WorkGraph) -> Vec<FlaggedItem> {
        let mut flagged = Vec::new();
        flagged.extend(self.detect_stale_items(graph));
        flagged.extend(self.detect_blocked_items(graph));
        flagged.extend(self.detect_resource_conflicts(graph));
        // Sort by ID for deterministic output
        flagged.sort_by(|a, b| a.id.cmp(&b.id));
        flagged
    }

    /// Detect items that have been in_progress too long.
    ///
    /// Note: Without timestamps in the current WorkItem model, we flag ALL
    /// in_progress items as potentially stale. The conductor service will
    /// track actual timestamps when running as a persistent process.
    /// For now, this flags items for human review.
    pub fn detect_stale_items(&self, graph: &WorkGraph) -> Vec<FlaggedItem> {
        graph
            .items_by_status("in_progress")
            .into_iter()
            .map(|item| FlaggedItem {
                id: item.id.clone(),
                title: item.title.clone(),
                assignee: item.assignee.clone(),
                reason: FlagReason::Stale {
                    status: "in_progress".to_string(),
                },
            })
            .collect()
    }

    /// Detect items blocked by incomplete dependencies.
    pub fn detect_blocked_items(&self, graph: &WorkGraph) -> Vec<FlaggedItem> {
        let mut flagged = Vec::new();

        for item in graph.items.values() {
            // Only check non-done items
            if item.status == "done" || item.status == "complete" {
                continue;
            }

            if let Some(deps) = graph.depends_on.get(&item.id) {
                let incomplete_deps: Vec<String> = deps
                    .iter()
                    .filter(|dep_id| {
                        graph
                            .items
                            .get(dep_id.as_str())
                            .is_some_and(|dep| dep.status != "done" && dep.status != "complete")
                    })
                    .cloned()
                    .collect();

                if !incomplete_deps.is_empty() {
                    flagged.push(FlaggedItem {
                        id: item.id.clone(),
                        title: item.title.clone(),
                        assignee: item.assignee.clone(),
                        reason: FlagReason::BlockedByDeps {
                            blockers: incomplete_deps,
                        },
                    });
                }
            }
        }

        flagged
    }

    /// Detect resource conflicts (GPU contention).
    ///
    /// If multiple in_progress items are tagged "gpu" and assigned to different
    /// agents, flag the conflict.
    pub fn detect_resource_conflicts(&self, graph: &WorkGraph) -> Vec<FlaggedItem> {
        let mut gpu_items: Vec<&WorkItem> = graph
            .items
            .values()
            .filter(|item| {
                item.status == "in_progress"
                    && item.tags.iter().any(|t| {
                        t.to_lowercase().contains("gpu") || t.to_lowercase().contains("training")
                    })
            })
            .collect();
        // Sort for deterministic "first holder" selection
        gpu_items.sort_by(|a, b| a.id.cmp(&b.id));

        // If more than one GPU item is in_progress, flag all but the first
        if gpu_items.len() <= 1 {
            return Vec::new();
        }

        let first_holder = gpu_items[0].assignee.as_deref().unwrap_or("unassigned");

        gpu_items[1..]
            .iter()
            .map(|item| FlaggedItem {
                id: item.id.clone(),
                title: item.title.clone(),
                assignee: item.assignee.clone(),
                reason: FlagReason::ResourceConflict {
                    resource: "GPU".to_string(),
                    held_by: first_holder.to_string(),
                },
            })
            .collect()
    }

    /// Generate a daily summary of the work graph.
    pub fn daily_summary(&self, graph: &WorkGraph) -> DailySummary {
        let completed: Vec<SummaryItem> = graph
            .items_by_status("done")
            .into_iter()
            .map(SummaryItem::from)
            .collect();

        let in_progress: Vec<SummaryItem> = graph
            .items_by_status("in_progress")
            .into_iter()
            .map(SummaryItem::from)
            .collect();

        let flagged = self.detect_issues(graph);
        let agent_states = self.tracker.agent_states(graph);
        let status_counts = graph.status_summary();

        DailySummary {
            completed,
            in_progress,
            flagged,
            agent_states,
            status_counts,
        }
    }
}

impl Default for BlockerMonitor {
    fn default() -> Self {
        Self::new()
    }
}

/// Format a daily summary as human-readable markdown.
pub fn format_daily_summary(summary: &DailySummary) -> String {
    let mut out = String::new();

    out.push_str("# Daily Board Summary\n\n");

    // Status overview
    out.push_str("## Status Overview\n\n");
    let mut statuses: Vec<_> = summary.status_counts.iter().collect();
    statuses.sort_by_key(|(k, _)| (*k).clone());
    for (status, count) in &statuses {
        out.push_str(&format!("  {status}: {count}\n"));
    }
    out.push('\n');

    // In progress
    if !summary.in_progress.is_empty() {
        out.push_str("## In Progress\n\n");
        for item in &summary.in_progress {
            let assignee = item.assignee.as_deref().unwrap_or("unassigned");
            out.push_str(&format!("  {} {} ({})\n", item.id, item.title, assignee));
        }
        out.push('\n');
    }

    // Flagged items
    if !summary.flagged.is_empty() {
        out.push_str("## Flagged Items\n\n");
        for item in &summary.flagged {
            out.push_str(&format!(
                "  ! {} {}{}\n",
                item.id, item.title, item.reason
            ));
        }
        out.push('\n');
    } else {
        out.push_str("## Flagged Items\n\n  None\n\n");
    }

    // Agent availability
    out.push_str("## Agent Status\n\n");
    for state in &summary.agent_states {
        let status = if state.available { "available" } else { "busy" };
        let wip = state.in_progress.len();
        out.push_str(&format!(
            "  {}{} ({} in-progress)\n",
            state.profile.name, status, wip
        ));
    }
    out.push('\n');

    // Completed count
    out.push_str(&format!(
        "## Completed: {} items\n",
        summary.completed.len()
    ));

    out
}

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

    fn make_item(
        id: &str,
        title: &str,
        status: &str,
        assignee: Option<&str>,
        tags: Vec<&str>,
        depends_on: Vec<&str>,
    ) -> WorkItem {
        WorkItem {
            id: id.to_string(),
            title: title.to_string(),
            item_type: "expedition".to_string(),
            status: status.to_string(),
            priority: Some("medium".to_string()),
            assignee: assignee.map(String::from),
            board: Some("development".to_string()),
            tags: tags.into_iter().map(String::from).collect(),
            related: vec![],
            depends_on: depends_on.into_iter().map(String::from).collect(),
            body: None,
        }
    }

    fn sample_graph() -> WorkGraph {
        WorkGraph::from_items(vec![
            make_item(
                "EX-100",
                "Arrow schemas",
                "done",
                Some("M5"),
                vec![],
                vec![],
            ),
            make_item(
                "EX-101",
                "NATS server",
                "in_progress",
                Some("DGX"),
                vec![],
                vec!["EX-100"],
            ),
            make_item(
                "EX-102",
                "Integration tests",
                "backlog",
                None,
                vec![],
                vec!["EX-101"],
            ),
            make_item(
                "EX-103",
                "GPU training pipeline",
                "in_progress",
                Some("DGX"),
                vec!["gpu", "training"],
                vec![],
            ),
            make_item(
                "EX-104",
                "GPU eval suite",
                "in_progress",
                Some("Mini"),
                vec!["gpu"],
                vec![],
            ),
            make_item("CH-200", "Clean up CI", "backlog", None, vec![], vec![]),
        ])
    }

    // ── Stale detection ─────────────────────────────────────────────────

    #[test]
    fn test_detect_stale_items() {
        let monitor = BlockerMonitor::new();
        let graph = sample_graph();

        let stale = monitor.detect_stale_items(&graph);
        // EX-101, EX-103, EX-104 are in_progress
        assert_eq!(stale.len(), 3);
        let ids: Vec<&str> = stale.iter().map(|f| f.id.as_str()).collect();
        assert!(ids.contains(&"EX-101"));
        assert!(ids.contains(&"EX-103"));
        assert!(ids.contains(&"EX-104"));
    }

    #[test]
    fn test_stale_items_excludes_done() {
        let monitor = BlockerMonitor::new();
        let graph = sample_graph();
        let stale = monitor.detect_stale_items(&graph);
        let ids: Vec<&str> = stale.iter().map(|f| f.id.as_str()).collect();
        assert!(!ids.contains(&"EX-100")); // done
        assert!(!ids.contains(&"CH-200")); // backlog
    }

    // ── Blocked detection ───────────────────────────────────────────────

    #[test]
    fn test_detect_blocked_items() {
        let monitor = BlockerMonitor::new();
        let graph = sample_graph();

        let blocked = monitor.detect_blocked_items(&graph);
        // EX-102 depends on EX-101 (in_progress)
        assert_eq!(blocked.len(), 1);
        assert_eq!(blocked[0].id, "EX-102");
        if let FlagReason::BlockedByDeps { blockers } = &blocked[0].reason {
            assert!(blockers.contains(&"EX-101".to_string()));
        } else {
            panic!("expected BlockedByDeps");
        }
    }

    #[test]
    fn test_blocked_clears_when_dep_done() {
        let monitor = BlockerMonitor::new();
        let mut graph = sample_graph();
        graph.apply_moved("EX-101", "done");

        let blocked = monitor.detect_blocked_items(&graph);
        // EX-102 depends on EX-101 which is now done → not blocked
        assert!(blocked.is_empty());
    }

    #[test]
    fn test_done_items_not_flagged_as_blocked() {
        let monitor = BlockerMonitor::new();
        let graph = sample_graph();
        let blocked = monitor.detect_blocked_items(&graph);
        let ids: Vec<&str> = blocked.iter().map(|f| f.id.as_str()).collect();
        assert!(!ids.contains(&"EX-100")); // done items never flagged
    }

    // ── Resource conflict detection ─────────────────────────────────────

    #[test]
    fn test_detect_gpu_conflict() {
        let monitor = BlockerMonitor::new();
        let graph = sample_graph();

        let conflicts = monitor.detect_resource_conflicts(&graph);
        // EX-103 (gpu, DGX) and EX-104 (gpu, Mini) are both in_progress
        assert_eq!(conflicts.len(), 1);
        assert_eq!(conflicts[0].id, "EX-104"); // second GPU item flagged
        if let FlagReason::ResourceConflict { resource, held_by } = &conflicts[0].reason {
            assert_eq!(resource, "GPU");
            assert_eq!(held_by, "DGX");
        }
    }

    #[test]
    fn test_no_gpu_conflict_single_item() {
        let monitor = BlockerMonitor::new();
        let graph = WorkGraph::from_items(vec![make_item(
            "EX-103",
            "GPU training",
            "in_progress",
            Some("DGX"),
            vec!["gpu"],
            vec![],
        )]);

        let conflicts = monitor.detect_resource_conflicts(&graph);
        assert!(conflicts.is_empty());
    }

    // ── Combined detection ──────────────────────────────────────────────

    #[test]
    fn test_detect_issues_all_types() {
        let monitor = BlockerMonitor::new();
        let graph = sample_graph();

        let issues = monitor.detect_issues(&graph);
        // Should have stale + blocked + GPU conflict
        assert!(issues.len() >= 4); // 3 stale + 1 blocked + 1 conflict (some overlap)
    }

    // ── Daily summary ───────────────────────────────────────────────────

    #[test]
    fn test_daily_summary_counts() {
        let monitor = BlockerMonitor::new();
        let graph = sample_graph();

        let summary = monitor.daily_summary(&graph);
        assert_eq!(summary.completed.len(), 1); // EX-100
        assert_eq!(summary.in_progress.len(), 3); // EX-101, 103, 104
        assert!(!summary.flagged.is_empty());
        assert_eq!(summary.agent_states.len(), 3); // M5, DGX, Mini
    }

    #[test]
    fn test_daily_summary_empty_graph() {
        let monitor = BlockerMonitor::new();
        let graph = WorkGraph::from_items(vec![]);

        let summary = monitor.daily_summary(&graph);
        assert!(summary.completed.is_empty());
        assert!(summary.in_progress.is_empty());
        assert!(summary.flagged.is_empty());
    }

    #[test]
    fn test_daily_summary_status_counts() {
        let monitor = BlockerMonitor::new();
        let graph = sample_graph();
        let summary = monitor.daily_summary(&graph);

        assert_eq!(summary.status_counts.get("done"), Some(&1));
        assert_eq!(summary.status_counts.get("in_progress"), Some(&3));
        assert_eq!(summary.status_counts.get("backlog"), Some(&2));
    }

    // ── Format ──────────────────────────────────────────────────────────

    #[test]
    fn test_format_daily_summary() {
        let monitor = BlockerMonitor::new();
        let graph = sample_graph();
        let summary = monitor.daily_summary(&graph);
        let formatted = format_daily_summary(&summary);

        assert!(formatted.contains("# Daily Board Summary"));
        assert!(formatted.contains("## Status Overview"));
        assert!(formatted.contains("## In Progress"));
        assert!(formatted.contains("## Flagged Items"));
        assert!(formatted.contains("## Agent Status"));
        assert!(formatted.contains("DGX"));
        assert!(formatted.contains("GPU"));
    }

    #[test]
    fn test_format_empty_summary() {
        let monitor = BlockerMonitor::new();
        let graph = WorkGraph::from_items(vec![]);
        let summary = monitor.daily_summary(&graph);
        let formatted = format_daily_summary(&summary);

        assert!(formatted.contains("Flagged Items"));
        assert!(formatted.contains("None"));
        assert!(formatted.contains("Completed: 0"));
    }

    // ── Config ──────────────────────────────────────────────────────────

    #[test]
    fn test_custom_config() {
        let config = MonitorConfig {
            stale_threshold: Duration::from_secs(3600), // 1 hour
        };
        let monitor = BlockerMonitor::with_config(config.clone());
        assert_eq!(monitor.config.stale_threshold, Duration::from_secs(3600));
    }

    #[test]
    fn test_default_config() {
        let config = MonitorConfig::default();
        assert_eq!(config.stale_threshold, Duration::from_secs(2 * 24 * 3600));
    }
}