STRA-338 size-fn: strata-control: worker generate and Generation::new within the limits (fn>30 2 -> 0)

This commit is contained in:
Pavlo Ratushnyi
2026-11-06 18:53:46 +01:00
parent 04d945da7a
commit 00ad202f28
2 changed files with 44 additions and 28 deletions
+3 -13
View File
@@ -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).
+41 -15
View File
@@ -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<u32>) {
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<BufReader<tokio::process::ChildStdout>>,
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,
};