batty-cli 0.11.63

Supervised agent execution for software teams
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
//! Manager dispatch-gap intervention: nudges idle managers when all their
//! reports are idle, there is no triage/review backlog, but there is
//! executable work available on the board or idle active tasks.

use std::time::Instant;

use anyhow::Result;
use tracing::{info, warn};

use super::super::*;
use super::{OwnedTaskInterventionState, task_needs_owned_intervention};

#[derive(Debug, Clone, PartialEq, Eq)]
struct ReportDispatchSnapshot {
    name: String,
    is_working: bool,
    active_task_ids: Vec<u32>,
}

impl TeamDaemon {
    pub(in super::super) fn maybe_intervene_manager_dispatch_gap(&mut self) -> Result<()> {
        if self
            .config
            .team_config
            .workflow_mode
            .suppresses_manager_relay()
        {
            return Ok(());
        }
        if !self
            .config
            .team_config
            .automation
            .manager_dispatch_interventions
        {
            return Ok(());
        }
        if super::super::super::pause_marker_path(&self.config.project_root).exists() {
            return Ok(());
        }
        if super::super::super::nudge_disabled_marker_path(&self.config.project_root, "dispatch")
            .exists()
        {
            return Ok(());
        }

        let board_dir = self
            .config
            .project_root
            .join(".batty")
            .join("team_config")
            .join("board");
        let inbox_root = inbox::inboxes_root(&self.config.project_root);
        let tasks = crate::task::load_tasks_from_dir(&board_dir.join("tasks"))?;
        let direct_reports =
            super::super::super::status::direct_reports_by_member(&self.config.members);
        let member_names: Vec<String> = self.config.pane_map.keys().cloned().collect();

        for name in member_names {
            let Some(member) = self
                .config
                .members
                .iter()
                .find(|member| member.name == name)
                .cloned()
            else {
                continue;
            };
            if member.role_type != RoleType::Manager {
                continue;
            }
            let stall_threshold = self.config.team_config.workflow_policy.stall_threshold_secs;
            let supervisory_stalled = self.is_supervisory_lane_stalled(&name, stall_threshold);

            let Some(reports) = direct_reports.get(&name) else {
                continue;
            };
            if reports.is_empty() {
                continue;
            }

            let triage_state = super::super::super::status::delivered_direct_report_triage_state(
                &inbox_root,
                &name,
                reports,
            )?;
            if triage_state.count > 0 {
                continue;
            }

            let review_count = tasks
                .iter()
                .filter(|task| {
                    super::review::actionable_review_backlog_owner_for_task(
                        task,
                        &self.config.members,
                    )
                    .as_deref()
                        == Some(name.as_str())
                })
                .count();
            if review_count > 0 {
                continue;
            }
            let manager_idle = self.member_idle_for_dispatch_gap(&name);

            let report_snapshots: Vec<ReportDispatchSnapshot> = reports
                .iter()
                .map(|report| ReportDispatchSnapshot {
                    name: report.clone(),
                    is_working: !self.member_idle_for_dispatch_gap(report),
                    active_task_ids: tasks
                        .iter()
                        .filter(|task| task.claimed_by.as_deref() == Some(report.as_str()))
                        .filter(|task| task_needs_owned_intervention(task.status.as_str()))
                        .map(|task| task.id)
                        .collect(),
                })
                .collect();

            if report_snapshots.iter().any(|snapshot| snapshot.is_working) {
                continue;
            }

            let idle_active_reports: Vec<&ReportDispatchSnapshot> = report_snapshots
                .iter()
                .filter(|snapshot| !snapshot.active_task_ids.is_empty())
                .collect();
            let idle_unassigned_reports: Vec<&ReportDispatchSnapshot> = report_snapshots
                .iter()
                .filter(|snapshot| snapshot.active_task_ids.is_empty())
                .collect();

            let dispatchable_task_ids: std::collections::HashSet<u32> =
                crate::team::resolver::engineer_dispatchable_tasks(
                    &board_dir,
                    &self.config.members,
                )?
                .into_iter()
                .map(|task| task.id)
                .collect();
            let mut unassigned_open_tasks: Vec<&crate::task::Task> = tasks
                .iter()
                .filter(|task| dispatchable_task_ids.contains(&task.id))
                .collect();
            unassigned_open_tasks
                .sort_by_key(|task| (manager_dispatch_priority_rank(&task.priority), task.id));

            if idle_active_reports.is_empty() && unassigned_open_tasks.is_empty() {
                continue;
            }

            let dispatch_gap_stall_reason = self.manager_dispatch_gap_stall_reason(
                &name,
                stall_threshold,
                manager_idle,
                &idle_unassigned_reports,
                &unassigned_open_tasks,
            );
            let dispatch_gap_stalled = supervisory_stalled || dispatch_gap_stall_reason.is_some();
            if supervisory_stalled {
                let reason = self.supervisory_progress_signal(&name, stall_threshold);
                self.record_supervisory_stall_reason(&name, stall_threshold, reason);
            } else if let Some((reason_suffix, short_label)) = dispatch_gap_stall_reason {
                self.record_manager_dispatch_gap_stall(
                    &name,
                    stall_threshold,
                    reason_suffix,
                    short_label,
                );
            }
            if !dispatch_gap_stalled && !manager_idle {
                continue;
            }
            if !dispatch_gap_stalled && !self.ready_for_idle_automation(&inbox_root, &name) {
                continue;
            }

            let dispatch_key = manager_dispatch_intervention_key(&name);
            let signature = manager_dispatch_intervention_signature(
                &idle_active_reports,
                &idle_unassigned_reports,
                &unassigned_open_tasks,
            );
            if self
                .owned_task_interventions
                .get(&dispatch_key)
                .is_some_and(|state| state.signature == signature)
            {
                continue;
            }
            if self.intervention_on_cooldown(&dispatch_key) {
                continue;
            }

            if dispatch_gap_stalled
                && !idle_unassigned_reports.is_empty()
                && !unassigned_open_tasks.is_empty()
            {
                let reason = if supervisory_stalled {
                    format!(
                        "manager_{}",
                        self.supervisory_progress_signal(&name, stall_threshold)
                            .stall_reason()
                    )
                } else {
                    let (reason_suffix, _) = dispatch_gap_stall_reason.unwrap();
                    format!("manager_supervisory_{reason_suffix}")
                };
                let fallback_count = self.fallback_direct_dispatch(
                    &name,
                    &reason,
                    &board_dir,
                    &idle_unassigned_reports,
                    &unassigned_open_tasks,
                )?;
                if fallback_count > 0 {
                    self.record_orchestrator_action(format!(
                        "recovery: dispatch fallback for {} assigned {} task(s) directly ({})",
                        name, fallback_count, reason
                    ));
                    let idle_epoch = self.triage_idle_epochs.get(&name).copied().unwrap_or(0);
                    self.owned_task_interventions.insert(
                        dispatch_key.clone(),
                        OwnedTaskInterventionState {
                            idle_epoch,
                            signature,
                            detected_at: Instant::now(),
                            escalation_sent: false,
                        },
                    );
                    self.intervention_cooldowns
                        .insert(dispatch_key, Instant::now());
                    continue;
                }
            }

            let text = self.build_manager_dispatch_gap_message(
                &member,
                &idle_active_reports,
                &idle_unassigned_reports,
                &unassigned_open_tasks,
            );
            info!(
                member = %name,
                idle_active_reports = idle_active_reports.len(),
                idle_unassigned_reports = idle_unassigned_reports.len(),
                unassigned_open_tasks = unassigned_open_tasks.len(),
                "firing manager dispatch-gap intervention"
            );
            let delivered_live = match self.queue_daemon_message(&name, &text) {
                Ok(MessageDelivery::LivePane) => true,
                Ok(_) => false,
                Err(error) => {
                    warn!(member = %name, error = %error, "failed to deliver manager dispatch-gap intervention");
                    continue;
                }
            };
            self.record_orchestrator_action(format!(
                "recovery: dispatch-gap intervention for {} (idle reports with active work: {}, unassigned reports: {}, open tasks: {})",
                name,
                idle_active_reports.len(),
                idle_unassigned_reports.len(),
                unassigned_open_tasks.len()
            ));
            let idle_epoch = self.triage_idle_epochs.get(&name).copied().unwrap_or(0);
            self.owned_task_interventions.insert(
                dispatch_key.clone(),
                OwnedTaskInterventionState {
                    idle_epoch,
                    signature,
                    detected_at: Instant::now(),
                    escalation_sent: false,
                },
            );
            self.intervention_cooldowns
                .insert(dispatch_key, Instant::now());
            if delivered_live {
                self.mark_member_working(&name);
            }
        }

        Ok(())
    }

    fn manager_dispatch_gap_stall_reason(
        &self,
        member_name: &str,
        threshold_secs: u64,
        member_idle: bool,
        idle_unassigned_reports: &[&ReportDispatchSnapshot],
        unassigned_open_tasks: &[&crate::task::Task],
    ) -> Option<(&'static str, &'static str)> {
        if threshold_secs == 0
            || member_idle
            || idle_unassigned_reports.is_empty()
            || unassigned_open_tasks.is_empty()
        {
            return None;
        }

        let handle = self.shim_handles.get(member_name)?;
        if handle.state != crate::shim::protocol::ShimState::Working
            || handle.secs_since_state_change() < threshold_secs
        {
            return None;
        }

        if handle
            .secs_since_last_activity()
            .is_some_and(|secs| secs < threshold_secs)
        {
            return Some(("shim_activity_only", "shim activity only"));
        }

        if self
            .watchers
            .get(member_name)
            .is_some_and(|watcher| watcher.secs_since_last_output_change() < threshold_secs)
        {
            return Some(("status_only_output", "status-only output"));
        }

        Some(("no_actionable_progress", "no actionable progress"))
    }

    fn member_idle_for_dispatch_gap(&self, member_name: &str) -> bool {
        self.shim_handles
            .get(member_name)
            .map(|handle| handle.state != crate::shim::protocol::ShimState::Working)
            .unwrap_or_else(|| self.is_member_idle(member_name))
    }

    fn record_manager_dispatch_gap_stall(
        &mut self,
        member_name: &str,
        stall_secs: u64,
        reason_suffix: &str,
        short_label: &str,
    ) {
        let cooldown_key = format!("supervisory-stall::{member_name}");
        let cooldown = std::time::Duration::from_secs(
            self.config
                .team_config
                .automation
                .intervention_cooldown_secs,
        );
        if self
            .intervention_cooldowns
            .get(&cooldown_key)
            .is_some_and(|last| last.elapsed() < cooldown)
        {
            return;
        }

        let observed_stall_secs = self
            .shim_handles
            .get(member_name)
            .map(|handle| handle.secs_since_state_change())
            .unwrap_or(stall_secs);
        let mut event = TeamEvent::stall_detected_with_reason(
            member_name,
            None,
            observed_stall_secs,
            Some(&format!("supervisory_stalled_manager_{reason_suffix}")),
        );
        event.task = Some(format!("supervisory::{member_name}"));
        event.details = Some(format!(
            "{member_name} (manager) stalled after {}: {short_label}",
            crate::team::status::format_health_duration(observed_stall_secs),
        ));
        self.emit_event(event);
        self.record_orchestrator_action(format!(
            "stall: detected {member_name} manager dispatch gap ({short_label})"
        ));
        self.intervention_cooldowns
            .insert(cooldown_key, Instant::now());
    }

    fn fallback_direct_dispatch(
        &mut self,
        manager_name: &str,
        reason: &str,
        board_dir: &std::path::Path,
        idle_unassigned_reports: &[&ReportDispatchSnapshot],
        unassigned_open_tasks: &[&crate::task::Task],
    ) -> Result<usize> {
        let mut dispatched = 0usize;
        for (report, task) in idle_unassigned_reports
            .iter()
            .zip(unassigned_open_tasks.iter())
            .take(1)
        {
            let assignment_message =
                format!("Task #{}: {}\n\n{}", task.id, task.title, task.description);
            crate::team::task_cmd::assign_task_owners(
                board_dir,
                task.id,
                Some(&report.name),
                None,
            )?;
            crate::team::task_cmd::transition_task_with_attribution(
                board_dir,
                task.id,
                "in-progress",
                crate::team::task_cmd::StatusTransitionAttribution::daemon(
                    "daemon.interventions.dispatch.fallback",
                ),
            )?;

            match self.assign_task_with_task_id_as(
                "daemon",
                &report.name,
                &assignment_message,
                Some(task.id),
            ) {
                Ok(_) => {
                    self.record_dispatch_fallback_used(manager_name, &report.name, task.id, reason);
                    dispatched += 1;
                }
                Err(error) => {
                    let _ = crate::team::task_cmd::transition_task_with_attribution(
                        board_dir,
                        task.id,
                        "todo",
                        crate::team::task_cmd::StatusTransitionAttribution::daemon(
                            "daemon.interventions.dispatch.rollback",
                        ),
                    );
                    let _ = crate::team::task_cmd::unclaim_task(board_dir, task.id);
                    warn!(
                        manager = %manager_name,
                        engineer = %report.name,
                        task_id = task.id,
                        error = %error,
                        "fallback direct-dispatch failed"
                    );
                }
            }
        }
        Ok(dispatched)
    }

    fn build_manager_dispatch_gap_message(
        &self,
        member: &MemberInstance,
        idle_active_reports: &[&ReportDispatchSnapshot],
        idle_unassigned_reports: &[&ReportDispatchSnapshot],
        unassigned_open_tasks: &[&crate::task::Task],
    ) -> String {
        let board_dir = self
            .config
            .project_root
            .join(".batty")
            .join("team_config")
            .join("board");
        let board_dir_str = board_dir.display();
        let active_report_summary = if idle_active_reports.is_empty() {
            "none".to_string()
        } else {
            idle_active_reports
                .iter()
                .map(|snapshot| {
                    let ids = snapshot
                        .active_task_ids
                        .iter()
                        .map(|id| format!("#{id}"))
                        .collect::<Vec<_>>()
                        .join(",");
                    format!("{} on {}", snapshot.name, ids)
                })
                .collect::<Vec<_>>()
                .join("; ")
        };
        let unassigned_report_summary = if idle_unassigned_reports.is_empty() {
            "none".to_string()
        } else {
            idle_unassigned_reports
                .iter()
                .map(|snapshot| snapshot.name.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        };
        let open_task_summary = if unassigned_open_tasks.is_empty() {
            "none".to_string()
        } else {
            unassigned_open_tasks
                .iter()
                .take(3)
                .map(|task| format!("#{} ({}) {}", task.id, task.status, task.title))
                .collect::<Vec<_>>()
                .join("; ")
        };
        let active_ids = idle_active_reports
            .iter()
            .flat_map(|snapshot| snapshot.active_task_ids.iter().copied())
            .collect::<std::collections::HashSet<_>>();
        let github_blockers = crate::task::load_tasks_from_dir(&board_dir.join("tasks"))
            .map(|tasks| {
                let active_tasks = tasks
                    .iter()
                    .filter(|task| active_ids.contains(&task.id))
                    .collect::<Vec<_>>();
                crate::team::github_feedback::active_github_blockers_for_tasks(
                    &self.config.project_root,
                    &active_tasks,
                )
            })
            .unwrap_or_default();

        let mut message = format!(
            "Dispatch recovery needed: you are idle, your reports are idle, and the lane has no triage/review backlog. Idle reports still holding active work: {active_report_summary}. Idle reports with no active task: {unassigned_report_summary}. Unassigned open board work: {open_task_summary}.\n\
            Recover the lane now:\n\
            1. `batty status`\n\
            2. `kanban-md list --dir {board_dir_str} --status in-progress`\n\
            3. `kanban-md list --dir {board_dir_str} --status todo`\n\
            4. `kanban-md list --dir {board_dir_str} --status backlog`"
        );

        if let Some(first_active) = idle_active_reports.first() {
            let first_task_id = first_active.active_task_ids[0];
            message.push_str(&format!(
                "\n5. For an idle active lane, intervene directly with `batty send {report} \"Task #{task_id} is idle under your ownership. Either move it forward now, report the exact blocker, or request board normalization.\"`.",
                report = first_active.name,
                task_id = first_task_id,
            ));
        }

        if let (Some(first_unassigned_report), Some(first_open_task)) = (
            idle_unassigned_reports.first(),
            unassigned_open_tasks.first(),
        ) {
            message.push_str(&format!(
                "\n6. If executable work exists, start it now with `batty assign {report} \"Task #{task_id}: {title}\"`.",
                report = first_unassigned_report.name,
                task_id = first_open_task.id,
                title = first_open_task.title,
            ));
        }

        if !github_blockers.is_empty() {
            let blocker_lines = github_blockers
                .iter()
                .map(|feedback| format!("- {}", feedback.intervention_line()))
                .collect::<Vec<_>>()
                .join("\n");
            message.push_str(&format!(
                "\nGitHub/CI verification blockers on idle active work:\n{blocker_lines}"
            ));
        }

        if let Some(parent) = &member.reports_to {
            message.push_str(&format!(
                "\n7. If the lane has no executable next step, escalate explicitly with `batty send {parent} \"lane blocked: all reports idle; need new dispatch or decision\"`."
            ));
        }

        message.push_str(
            "\nDo not let the entire lane sit idle. Either wake an active task, assign new executable work, or escalate the exact blockage now.",
        );
        self.prepend_member_nudge(member, message)
    }
}

pub(super) fn manager_dispatch_intervention_key(member_name: &str) -> String {
    format!("dispatch::{member_name}")
}

fn manager_dispatch_priority_rank(priority: &str) -> u32 {
    match priority {
        "critical" => 0,
        "high" => 1,
        "medium" => 2,
        "low" => 3,
        _ => 4,
    }
}

pub(super) fn manager_dispatch_intervention_signature(
    idle_active_reports: &[&ReportDispatchSnapshot],
    idle_unassigned_reports: &[&ReportDispatchSnapshot],
    unassigned_open_tasks: &[&crate::task::Task],
) -> String {
    let mut parts = Vec::new();
    for snapshot in idle_active_reports {
        let task_ids = snapshot
            .active_task_ids
            .iter()
            .map(u32::to_string)
            .collect::<Vec<_>>()
            .join(",");
        parts.push(format!("active:{}:{task_ids}", snapshot.name));
    }
    for snapshot in idle_unassigned_reports {
        parts.push(format!("idle:{}", snapshot.name));
    }
    for task in unassigned_open_tasks {
        parts.push(format!("open:{}:{}", task.id, task.status));
    }
    parts.sort();
    parts.join("|")
}

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

    #[test]
    fn dispatch_key_uses_dispatch_prefix() {
        assert_eq!(manager_dispatch_intervention_key("lead"), "dispatch::lead");
        assert_eq!(
            manager_dispatch_intervention_key("mgr-2"),
            "dispatch::mgr-2"
        );
    }

    #[test]
    fn dispatch_signature_includes_active_reports_with_task_ids() {
        let active = ReportDispatchSnapshot {
            name: "eng-1".to_string(),
            is_working: false,
            active_task_ids: vec![10, 20],
        };
        let sig = manager_dispatch_intervention_signature(&[&active], &[], &[]);
        assert_eq!(sig, "active:eng-1:10,20");
    }

    #[test]
    fn dispatch_signature_includes_idle_and_open_components() {
        let idle = ReportDispatchSnapshot {
            name: "eng-2".to_string(),
            is_working: false,
            active_task_ids: vec![],
        };
        let task = crate::task::Task {
            id: 50,
            title: "open-task".to_string(),
            status: "todo".to_string(),
            priority: "high".to_string(),
            assignee: None,
            claimed_by: None,
            claimed_at: None,
            claim_ttl_secs: None,
            claim_expires_at: None,
            last_progress_at: None,
            claim_warning_sent_at: None,
            claim_extensions: None,
            last_output_bytes: None,
            blocked: None,
            tags: Vec::new(),
            depends_on: Vec::new(),
            review_owner: None,
            blocked_on: None,
            worktree_path: None,
            branch: None,
            commit: None,
            artifacts: Vec::new(),
            next_action: None,
            scheduled_for: None,
            cron_schedule: None,
            cron_last_run: None,
            completed: None,
            description: String::new(),
            batty_config: None,
            source_path: std::path::PathBuf::from("task-50.md"),
        };
        let sig = manager_dispatch_intervention_signature(&[], &[&idle], &[&task]);
        assert_eq!(sig, "idle:eng-2|open:50:todo");
    }

    #[test]
    fn dispatch_signature_empty_inputs_returns_empty() {
        let sig = manager_dispatch_intervention_signature(&[], &[], &[]);
        assert_eq!(sig, "");
    }

    #[test]
    fn dispatch_signature_sorts_all_components() {
        let active = ReportDispatchSnapshot {
            name: "eng-z".to_string(),
            is_working: false,
            active_task_ids: vec![5],
        };
        let idle = ReportDispatchSnapshot {
            name: "eng-a".to_string(),
            is_working: false,
            active_task_ids: vec![],
        };
        let task = crate::task::Task {
            id: 1,
            title: "task".to_string(),
            status: "backlog".to_string(),
            priority: "high".to_string(),
            assignee: None,
            claimed_by: None,
            claimed_at: None,
            claim_ttl_secs: None,
            claim_expires_at: None,
            last_progress_at: None,
            claim_warning_sent_at: None,
            claim_extensions: None,
            last_output_bytes: None,
            blocked: None,
            tags: Vec::new(),
            depends_on: Vec::new(),
            review_owner: None,
            blocked_on: None,
            worktree_path: None,
            branch: None,
            commit: None,
            artifacts: Vec::new(),
            next_action: None,
            scheduled_for: None,
            cron_schedule: None,
            cron_last_run: None,
            completed: None,
            description: String::new(),
            batty_config: None,
            source_path: std::path::PathBuf::from("task-1.md"),
        };
        let sig = manager_dispatch_intervention_signature(&[&active], &[&idle], &[&task]);
        assert_eq!(sig, "active:eng-z:5|idle:eng-a|open:1:backlog");
    }

    #[test]
    fn dispatch_signature_multiple_active_reports() {
        let a1 = ReportDispatchSnapshot {
            name: "eng-2".to_string(),
            is_working: false,
            active_task_ids: vec![30],
        };
        let a2 = ReportDispatchSnapshot {
            name: "eng-1".to_string(),
            is_working: false,
            active_task_ids: vec![10, 20],
        };
        let sig = manager_dispatch_intervention_signature(&[&a1, &a2], &[], &[]);
        // Should sort: active:eng-1:10,20 before active:eng-2:30
        assert_eq!(sig, "active:eng-1:10,20|active:eng-2:30");
    }

    #[test]
    fn dispatch_priority_rank_orders_named_priorities() {
        assert_eq!(manager_dispatch_priority_rank("critical"), 0);
        assert_eq!(manager_dispatch_priority_rank("high"), 1);
        assert_eq!(manager_dispatch_priority_rank("medium"), 2);
        assert_eq!(manager_dispatch_priority_rank("low"), 3);
        assert_eq!(manager_dispatch_priority_rank("unknown"), 4);
    }
}