diff --git a/control-plane/src/worker/combine_done.rs b/control-plane/src/worker/combine_done.rs index 1b7765d9..c3b24c43 100644 --- a/control-plane/src/worker/combine_done.rs +++ b/control-plane/src/worker/combine_done.rs @@ -52,19 +52,9 @@ pub(super) async fn generate( write_frame(pipe, request.as_bytes()).await?; let stop_deadline = tokio::time::sleep(STOP_DRAIN_TIMEOUT); tokio::pin!(stop_deadline); - let mut run = Generation::new(command, status, stall_timeout); - loop { - let Some(line) = run.next_line(pipe, lines, stop_deadline.as_mut()).await? else { - continue; - }; - if run - .on_line(&line, pipe, stop_deadline.as_mut()) - .await? - .is_break() - { - return Ok(()); - } - } + Generation::new(command, status, stall_timeout) + .pump(pipe, lines, stop_deadline.as_mut()) + .await } /// Hand one event to the client side (buffered or streamed). diff --git a/control-plane/src/worker/generation.rs b/control-plane/src/worker/generation.rs index 2cf9eb6e..df968a22 100644 --- a/control-plane/src/worker/generation.rs +++ b/control-plane/src/worker/generation.rs @@ -101,6 +101,23 @@ impl StallWatch { } } +/// Reasoning budget: the thinking is wrapped up at the budget and generation goes on. +fn reasoning_budget( + command: &GenerationCommand, +) -> (Option<(BudgetTracker, wire::ReasoningPlan)>, Vec) { + let budget = command + .request + .reasoning + .clone() + .map(|plan| (BudgetTracker::new(plan.budget), plan)); + let prompt = if budget.is_some() { + command.request.token_ids.clone() + } else { + Vec::new() + }; + (budget, prompt) +} + /// The state one generation carries from line to line, including the reasoning-budget passes (issue #123). pub(super) struct Generation<'a> { pub(super) command: &'a mut GenerationCommand, @@ -129,17 +146,7 @@ impl<'a> Generation<'a> { ) -> Self { let started = command.request.obs.now(); let watch = StallWatch::new(stall_timeout); - // Reasoning budget: the thinking is wrapped up at the budget and generation goes on. - let budget = command - .request - .reasoning - .clone() - .map(|plan| (BudgetTracker::new(plan.budget), plan)); - let prompt = if budget.is_some() { - command.request.token_ids.clone() - } else { - Vec::new() - }; + let (budget, prompt) = reasoning_budget(command); Self { command, status, @@ -157,6 +164,27 @@ impl<'a> Generation<'a> { } } + /// Read engine lines until the request ends: `Ok` on a terminal line, `Err` on a protocol failure. + pub(super) async fn pump( + mut self, + pipe: &mut ChildStdin, + lines: &mut tokio::io::Lines>, + mut deadline: Pin<&mut Sleep>, + ) -> Result<(), String> { + loop { + let Some(line) = self.next_line(pipe, lines, deadline.as_mut()).await? else { + continue; + }; + if self + .on_line(&line, pipe, deadline.as_mut()) + .await? + .is_break() + { + return Ok(()); + } + } + } + /// A STOP is in flight: the engine has to answer it before the deadline. fn draining(&self) -> bool { self.stopped || self.wrap_pending @@ -175,13 +203,11 @@ impl<'a> Generation<'a> { return Err("engine did not terminate generation within 20 seconds of STOP".to_owned()); } _ = self.command.result.closed(), if !self.stopped => { - self.client_closed(pipe, deadline.as_mut()).await?; - return Ok(None); + return self.client_closed(pipe, deadline.as_mut()).await.map(|()| None); } _ = self.watch.ticker.tick(), if self.watch.dog.is_some() => { let elapsed = self.started.elapsed().as_millis(); - self.watch.poll(u64::try_from(elapsed).unwrap_or(u64::MAX))?; - return Ok(None); + return self.watch.poll(u64::try_from(elapsed).unwrap_or(u64::MAX)).map(|()| None); } line = lines.next_line() => line, };