refactor(agent): build the InfiniteSession once in the serve loop, thread it

This commit is contained in:
müde 2026-07-05 20:16:06 +02:00
commit c9de2eeb97
2 changed files with 32 additions and 12 deletions

View file

@ -503,6 +503,9 @@ async fn serve_loop<S: Surface>(
) -> Result<()> {
tracing::info!(socket = %socket.display(), "harness serve");
S::requeue_inflight(socket).await;
// The durable claude session, built once and reused for every turn +
// idle compaction below (it's effectively stateless).
let session = turn::make_session(&bus);
// Set when a turn calls `request_next_turn` and no real work is
// pending — the next iteration drives this synthetic message
// in-process instead of long-polling the broker. Never
@ -519,7 +522,7 @@ async fn serve_loop<S: Surface>(
// (the in-flight case is handled at the end of drive_turn).
let compacted = {
let _guard = turn_lock.lock().await;
turn::run_pending_compact(files, &bus).await
turn::run_pending_compact(files, &bus, &session).await
};
if !compacted {
tokio::time::sleep(interval).await;
@ -545,6 +548,7 @@ async fn serve_loop<S: Surface>(
stats.as_ref(),
files,
&turn_lock,
&session,
graceful_stop_message(),
)
.await;
@ -553,7 +557,8 @@ async fn serve_loop<S: Surface>(
}
},
};
let ctrl = handle_turn::<S>(socket, &bus, stats.as_ref(), files, &turn_lock, next).await;
let ctrl =
handle_turn::<S>(socket, &bus, stats.as_ref(), files, &turn_lock, &session, next).await;
if ctrl.auth_failed {
*login_state.lock().unwrap() = LoginState::NeedsLogin;
login::wait_for_login(
@ -582,6 +587,7 @@ async fn handle_turn<S: Surface>(
stats: Option<&TurnStats>,
files: &turn::TurnFiles,
turn_lock: &TurnLock,
session: &turn::AgentSession,
first: hive_sh4re::DeliveredMessage,
) -> TurnControl {
let from = first.from;
@ -603,7 +609,7 @@ async fn handle_turn<S: Surface>(
let prompt = serve_common::format_wake_prompt(msg_id, &from, &body, unread, redelivered);
let outcome = {
let _guard = turn_lock.lock().await;
turn::drive_turn(&prompt, files, bus).await
turn::drive_turn(&prompt, files, bus, session).await
};
turn::emit_turn_end(bus, &outcome);
bus.set_state(TurnState::Idle);