diff --git a/hive-ag3nt/src/turn.rs b/hive-ag3nt/src/turn.rs index 73fa2cbd..8e5ceda0 100644 --- a/hive-ag3nt/src/turn.rs +++ b/hive-ag3nt/src/turn.rs @@ -306,18 +306,11 @@ pub async fn drive_turn(prompt: &str, files: &TurnFiles, bus: &Bus) -> TurnOutco maybe_auto_reset(bus); let outcome = match run_turn(prompt, files, bus).await { TurnOutcome::PromptTooLong => { - // Compact has its own three-flag surface (it's the same claude - // binary). Treat any non-Ok outcome the same as if `run_turn` - // had returned it — the serve loop already knows what to do - // with each variant, no point re-wrapping. PromptTooLong from - // /compact itself would be absurd recursion; bubble it up as - // a normal failure path. - match compact_session(files, bus).await { - TurnOutcome::Ok | TurnOutcome::Compacted => { - run_turn(prompt, files, bus).await - } - other => return other, + if let Err(e) = compact_session(files, bus).await { + tracing::warn!(error = %format!("{e:#}"), "compact failed"); + return TurnOutcome::Failed(e); } + run_turn(prompt, files, bus).await } // Rate-limited: no point retrying immediately — bubble up so the // serve loop can park + emit status before the next attempt. @@ -377,28 +370,11 @@ async fn maybe_checkpoint_and_compact(files: &TurnFiles, bus: &Bus) -> bool { text: format!("checkpoint turn failed ({e:#}) — compacting anyway"), }), } - // Best-effort: never changes the outcome of the turn that already - // succeeded. Mirror the checkpoint-turn handling above — emit a Note - // for each failure mode and move on; the next real turn will surface - // the underlying issue (rate-limit / 401 / etc.) through the normal - // path anyway. - match compact_session(files, bus).await { - TurnOutcome::Ok | TurnOutcome::Compacted => {} - TurnOutcome::PromptTooLong => bus.emit(LiveEvent::Note { - text: "/compact unexpectedly returned PromptTooLong — next turn will retry".into(), - }), - TurnOutcome::RateLimited => bus.emit(LiveEvent::Note { - text: "/compact was rate-limited — next turn will park + retry".into(), - }), - TurnOutcome::AuthFailed => bus.emit(LiveEvent::Note { - text: "/compact hit 401 — next turn will trigger the re-login flow".into(), - }), - TurnOutcome::Failed(e) => { - tracing::warn!(error = %format!("{e:#}"), "post-checkpoint compact failed"); - bus.emit(LiveEvent::Note { - text: format!("/compact after checkpoint failed: {e:#}"), - }); - } + if let Err(e) = compact_session(files, bus).await { + tracing::warn!(error = %format!("{e:#}"), "post-checkpoint compact failed"); + bus.emit(LiveEvent::Note { + text: format!("/compact after checkpoint failed: {e:#}"), + }); } true } @@ -530,42 +506,19 @@ pub async fn run_turn(prompt: &str, files: &TurnFiles, bus: &Bus) -> TurnOutcome /// compact state matches a normal turn's. Only the prompt over stdin /// differs (`/compact` vs the wake-up payload). /// -/// Returns the same `TurnOutcome` shape as `run_turn` so callers can -/// react identically to all three failure flags (`prompt_too_long`, -/// `rate_limited`, `auth_failed`). The reactive caller bubbles any -/// non-Ok outcome up so the serve loop's normal handling (park + -/// retry on rate-limit, flip to `needs_login` on 401, etc.) kicks in; -/// the proactive post-checkpoint caller stays best-effort and only -/// emits a Note for each failure mode. -pub async fn compact_session(files: &TurnFiles, bus: &Bus) -> TurnOutcome { +/// # Errors +/// +/// Returns an error if the `claude --print /compact` invocation fails +/// (non-zero exit or I/O error). +pub async fn compact_session(files: &TurnFiles, bus: &Bus) -> Result<()> { bus.emit(LiveEvent::Note { text: "context overflow — running /compact on the persistent session".into(), }); - let outcome = match run_claude("/compact", files, bus).await { - Ok((true, _, _)) => TurnOutcome::PromptTooLong, - Ok((_, true, _)) => TurnOutcome::RateLimited, - Ok((_, _, true)) => TurnOutcome::AuthFailed, - Ok(_) => TurnOutcome::Ok, - Err(e) => TurnOutcome::Failed(e), - }; - match &outcome { - TurnOutcome::Ok | TurnOutcome::Compacted => bus.emit(LiveEvent::Note { - text: "/compact done".into(), - }), - TurnOutcome::PromptTooLong => bus.emit(LiveEvent::Note { - text: "/compact reported PromptTooLong — bubbling up".into(), - }), - TurnOutcome::RateLimited => bus.emit(LiveEvent::Note { - text: "/compact was rate-limited — bubbling up".into(), - }), - TurnOutcome::AuthFailed => bus.emit(LiveEvent::Note { - text: "/compact hit 401 — bubbling up for re-login flow".into(), - }), - TurnOutcome::Failed(e) => bus.emit(LiveEvent::Note { - text: format!("/compact failed: {e:#}"), - }), - } - outcome + let (_, _, _) = run_claude("/compact", files, bus).await?; + bus.emit(LiveEvent::Note { + text: "/compact done".into(), + }); + Ok(()) } #[allow(clippy::too_many_lines)] diff --git a/hive-ag3nt/src/web_ui.rs b/hive-ag3nt/src/web_ui.rs index d53c5637..14e02e39 100644 --- a/hive-ag3nt/src/web_ui.rs +++ b/hive-ag3nt/src/web_ui.rs @@ -851,13 +851,12 @@ async fn post_compact(State(state): State) -> Response { text: "operator: /compact — running on persistent session".into(), }); bus.set_state(crate::events::TurnState::Compacting); - let outcome = crate::turn::compact_session(&files, &bus).await; + let r = crate::turn::compact_session(&files, &bus).await; bus.set_state(crate::events::TurnState::Idle); - // Best-effort manual /compact from the operator: compact_session - // already emits a Note per outcome, so we don't need to re-emit - // here — just record any underlying error to the harness log. - if let crate::turn::TurnOutcome::Failed(e) = outcome { - tracing::warn!(error = %format!("{e:#}"), "operator /compact failed"); + if let Err(e) = r { + bus.emit(crate::events::LiveEvent::Note { + text: format!("/compact failed: {e:#}"), + }); } }); (axum::http::StatusCode::OK, "ok").into_response()