feat: add ecc2 global coordination action

This commit is contained in:
Affaan Mustafa
2026-04-08 02:46:01 -07:00
parent 6dc5577319
commit 38f502299a
3 changed files with 179 additions and 1 deletions

View File

@@ -90,6 +90,18 @@ enum Commands {
#[arg(long, default_value_t = 10)]
lead_limit: usize,
},
/// Dispatch unread handoffs, then rebalance delegate backlog across lead teams
CoordinateBacklog {
/// Agent type for routed delegates
#[arg(short, long, default_value = "claude")]
agent: String,
/// Create a dedicated worktree if new delegates must be spawned
#[arg(short, long, default_value_t = true)]
worktree: bool,
/// Maximum lead sessions to sweep in one pass
#[arg(long, default_value_t = 10)]
lead_limit: usize,
},
/// Rebalance unread handoffs across lead teams with backed-up delegates
RebalanceAll {
/// Agent type for routed delegates
@@ -349,6 +361,47 @@ async fn main() -> Result<()> {
}
}
}
Some(Commands::CoordinateBacklog {
agent,
worktree: use_worktree,
lead_limit,
}) => {
let dispatch_outcomes = session::manager::auto_dispatch_backlog(
&db,
&cfg,
&agent,
use_worktree,
lead_limit,
)
.await?;
let total_routed: usize =
dispatch_outcomes.iter().map(|outcome| outcome.routed.len()).sum();
let rebalance_outcomes = session::manager::rebalance_all_teams(
&db,
&cfg,
&agent,
use_worktree,
lead_limit,
)
.await?;
let total_rerouted: usize = rebalance_outcomes
.iter()
.map(|outcome| outcome.rerouted.len())
.sum();
if total_routed == 0 && total_rerouted == 0 {
println!("Backlog already clear");
} else {
println!(
"Coordinated backlog: dispatched {} handoff(s) across {} lead(s); rebalanced {} handoff(s) across {} lead(s)",
total_routed,
dispatch_outcomes.len(),
total_rerouted,
rebalance_outcomes.len()
);
}
}
Some(Commands::RebalanceAll {
agent,
worktree: use_worktree,
@@ -791,6 +844,31 @@ mod tests {
}
}
#[test]
fn cli_parses_coordinate_backlog_command() {
let cli = Cli::try_parse_from([
"ecc",
"coordinate-backlog",
"--agent",
"claude",
"--lead-limit",
"7",
])
.expect("coordinate-backlog should parse");
match cli.command {
Some(Commands::CoordinateBacklog {
agent,
lead_limit,
..
}) => {
assert_eq!(agent, "claude");
assert_eq!(lead_limit, 7);
}
_ => panic!("expected coordinate-backlog subcommand"),
}
}
#[test]
fn cli_parses_rebalance_all_command() {
let cli = Cli::try_parse_from([

View File

@@ -44,6 +44,7 @@ pub async fn run(db: StateStore, cfg: Config) -> Result<()> {
(_, KeyCode::Char('B')) => dashboard.rebalance_all_teams().await,
(_, KeyCode::Char('i')) => dashboard.drain_inbox_selected().await,
(_, KeyCode::Char('g')) => dashboard.auto_dispatch_backlog().await,
(_, KeyCode::Char('G')) => dashboard.coordinate_backlog().await,
(_, KeyCode::Char('p')) => dashboard.toggle_auto_dispatch_policy(),
(_, KeyCode::Char(',')) => dashboard.adjust_auto_dispatch_limit(-1),
(_, KeyCode::Char('.')) => dashboard.adjust_auto_dispatch_limit(1),

View File

@@ -407,7 +407,7 @@ impl Dashboard {
fn render_status_bar(&self, frame: &mut Frame, area: Rect) {
let text = format!(
" [n]ew session [a]ssign re[b]alance global re[B]alance dra[i]n inbox [g]lobal dispatch toggle [p]olicy [,/.] dispatch limit [s]top [u]resume [x]cleanup [d]elete [r]efresh [Tab] switch pane [j/k] scroll [+/-] resize [{}] layout [?] help [q]uit ",
" [n]ew session [a]ssign re[b]alance global re[B]alance dra[i]n inbox [g]lobal dispatch coordinate [G]lobal toggle [p]olicy [,/.] dispatch limit [s]top [u]resume [x]cleanup [d]elete [r]efresh [Tab] switch pane [j/k] scroll [+/-] resize [{}] layout [?] help [q]uit ",
self.layout_label()
);
let text = if let Some(note) = self.operator_note.as_ref() {
@@ -457,6 +457,7 @@ impl Dashboard {
" B Rebalance backed-up delegate inboxes across lead teams",
" i Drain unread task handoffs from selected session inbox",
" g Auto-dispatch unread handoffs across lead sessions",
" G Dispatch then rebalance backlog across lead teams",
" p Toggle daemon auto-dispatch policy and persist config",
" ,/. Decrease/increase auto-dispatch limit per lead",
" s Stop selected session",
@@ -887,6 +888,75 @@ impl Dashboard {
}
}
pub async fn coordinate_backlog(&mut self) {
let agent = self.cfg.default_agent.clone();
let lead_limit = self.sessions.len().max(1);
let dispatch_outcomes = match manager::auto_dispatch_backlog(
&self.db,
&self.cfg,
&agent,
true,
lead_limit,
)
.await
{
Ok(outcomes) => outcomes,
Err(error) => {
tracing::warn!("Failed to coordinate backlog dispatch from dashboard: {error}");
self.set_operator_note(format!("global coordinate failed during dispatch: {error}"));
return;
}
};
let total_routed: usize = dispatch_outcomes.iter().map(|outcome| outcome.routed.len()).sum();
let rebalance_outcomes = match manager::rebalance_all_teams(
&self.db,
&self.cfg,
&agent,
true,
lead_limit,
)
.await
{
Ok(outcomes) => outcomes,
Err(error) => {
tracing::warn!("Failed to coordinate backlog rebalance from dashboard: {error}");
self.set_operator_note(format!("global coordinate failed during rebalance: {error}"));
return;
}
};
let total_rerouted: usize = rebalance_outcomes
.iter()
.map(|outcome| outcome.rerouted.len())
.sum();
let selected_session_id = self
.sessions
.get(self.selected_session)
.map(|session| session.id.clone());
self.refresh();
self.sync_selection_by_id(selected_session_id.as_deref());
self.sync_selected_output();
self.sync_selected_diff();
self.sync_selected_messages();
self.sync_selected_lineage();
self.refresh_logs();
if total_routed == 0 && total_rerouted == 0 {
self.set_operator_note("backlog already clear".to_string());
} else {
self.set_operator_note(format!(
"coordinated backlog: dispatched {} handoff(s) across {} lead(s), rebalanced {} handoff(s) across {} lead(s)",
total_routed,
dispatch_outcomes.len(),
total_rerouted,
rebalance_outcomes.len()
));
}
}
pub async fn stop_selected(&mut self) {
let Some(session) = self.sessions.get(self.selected_session) else {
return;
@@ -2444,6 +2514,35 @@ mod tests {
Ok(())
}
#[tokio::test]
async fn coordinate_backlog_sets_operator_note_when_clear() -> Result<()> {
let db_path = std::env::temp_dir().join(format!("ecc2-dashboard-{}.db", Uuid::new_v4()));
let db = StateStore::open(&db_path)?;
let now = Utc::now();
db.insert_session(&Session {
id: "lead-1".to_string(),
task: "coordinate".to_string(),
agent_type: "claude".to_string(),
working_dir: PathBuf::from("/tmp"),
state: SessionState::Running,
pid: None,
worktree: None,
created_at: now,
updated_at: now,
metrics: SessionMetrics::default(),
})?;
let dashboard_store = StateStore::open(&db_path)?;
let mut dashboard = Dashboard::new(dashboard_store, Config::default());
dashboard.coordinate_backlog().await;
assert_eq!(dashboard.operator_note.as_deref(), Some("backlog already clear"));
let _ = std::fs::remove_file(db_path);
Ok(())
}
#[test]
fn grid_layout_renders_four_panes() {
let mut dashboard = test_dashboard(vec![sample_session("grid-1", "claude", SessionState::Running, None, 1, 1)], 0);