dedupe: lift drive_turn/emit_turn_end/wait_for_login into hive_ag3nt::turn

This commit is contained in:
müde 2026-05-15 16:27:51 +02:00
parent 8fbee4fbf2
commit e9b213690e
3 changed files with 81 additions and 140 deletions

View file

@ -92,11 +92,8 @@ async fn main() -> Result<()> {
match initial {
LoginState::Online => serve(&cli.socket, Duration::from_millis(poll_ms), bus).await,
LoginState::NeedsLogin => {
tracing::warn!(
claude_dir = %claude_dir.display(),
"manager has no claude session — staying in partial-run mode"
);
needs_login_loop(&cli.socket, &claude_dir, login_state, poll_ms, bus).await
turn::wait_for_login(&claude_dir, login_state, poll_ms).await;
serve(&cli.socket, Duration::from_millis(poll_ms), bus).await
}
}
}
@ -126,26 +123,6 @@ async fn one_shot(socket: &Path, req: ManagerRequest) -> Result<()> {
Ok(())
}
/// Manager-side mirror of hive-ag3nt's needs-login loop: keep the web UI
/// alive, poll the claude dir, enter `serve` once login lands.
async fn needs_login_loop(
socket: &Path,
claude_dir: &Path,
state: Arc<Mutex<LoginState>>,
poll_ms: u64,
bus: Bus,
) -> Result<()> {
let probe = Duration::from_millis(poll_ms.max(2000));
loop {
tokio::time::sleep(probe).await;
if login::has_session(claude_dir) {
tracing::info!("manager claude session detected — entering inbox loop");
*state.lock().unwrap() = LoginState::Online;
return serve(socket, Duration::from_millis(poll_ms), bus).await;
}
}
}
async fn serve(socket: &Path, interval: Duration, bus: Bus) -> Result<()> {
tracing::info!(socket = %socket.display(), "hive-m1nd serve");
let mcp_config = turn::write_mcp_config(socket).await?;
@ -172,8 +149,9 @@ async fn serve(socket: &Path, interval: Duration, bus: Bus) -> Result<()> {
body: body.clone(),
});
let prompt = format_wake_prompt(&label, &from, &body);
let outcome = drive_turn(&prompt, &mcp_config, &bus, mcp::Flavor::Manager).await;
emit_turn_end(&bus, &outcome);
let outcome =
turn::drive_turn(&prompt, &mcp_config, &bus, mcp::Flavor::Manager).await;
turn::emit_turn_end(&bus, &outcome);
}
Ok(ManagerResponse::Empty) => {}
Ok(ManagerResponse::Ok | ManagerResponse::Status { .. }) => {
@ -190,46 +168,6 @@ async fn serve(socket: &Path, interval: Duration, bus: Bus) -> Result<()> {
}
}
/// Drive one manager turn end-to-end with the same overflow-then-compact
/// retry as sub-agents.
async fn drive_turn(
prompt: &str,
mcp_config: &Path,
bus: &Bus,
flavor: mcp::Flavor,
) -> turn::TurnOutcome {
match turn::run_turn(prompt, mcp_config, bus, flavor).await {
turn::TurnOutcome::PromptTooLong => {
if let Err(e) = turn::compact_session(bus).await {
tracing::warn!(error = %format!("{e:#}"), "compact failed");
return turn::TurnOutcome::Failed(e);
}
turn::run_turn(prompt, mcp_config, bus, flavor).await
}
other => other,
}
}
fn emit_turn_end(bus: &Bus, outcome: &turn::TurnOutcome) {
match outcome {
turn::TurnOutcome::Ok | turn::TurnOutcome::PromptTooLong => {
bus.emit(LiveEvent::TurnEnd {
ok: true,
note: None,
});
tracing::info!("manager turn finished");
}
turn::TurnOutcome::Failed(e) => {
let note = format!("{e:#}");
bus.emit(LiveEvent::TurnEnd {
ok: false,
note: Some(note.clone()),
});
tracing::warn!(error = %note, "manager turn failed");
}
}
}
/// Manager-flavored wake prompt. Mentions the privileged tools the sub-agent
/// prompt doesn't have access to, and points the manager at its own
/// editable config repo for self-modification.