STRA-338 size: engine verify_commit within limits (commit_parts)

This commit is contained in:
Pavlo Ratushnyi
2026-11-06 15:42:56 +01:00
parent 880f90e84b
commit cbf9f166ef
6 changed files with 131 additions and 76 deletions
@@ -124,7 +124,7 @@ use run_parts::{
#[path = "runner/command_loop.rs"]
mod command_loop;
#[allow(unused_imports)]
use command_loop::{LoopCtx, announce, command_loop};
use command_loop::{LoopCtx, announce, answer_unload, command_loop};
#[path = "runner/reconfigure_parts.rs"]
mod reconfigure_parts;
#[allow(unused_imports)]
@@ -89,3 +89,13 @@ fn handle_command<'a, 'ctx>(
}
Ok(None)
}
/// A failed close stops the engine; the unload (if that ended the loop) is answered with the close's result.
pub(super) fn answer_unload(shared: &Shared, closed: Result<(), BackendError>, unload_reply: Option<Reply<()>>) {
if closed.is_err() {
shared.shutdown.store(true, Ordering::Release);
}
if let Some(reply) = unload_reply {
let _ = reply.send(closed);
}
}
@@ -32,12 +32,6 @@ pub(in super::super::super) fn run(args: Run<'_, '_>) -> Result<(), BackendError
let stack_closed = close_stack(stack.take());
// the stack's borrow of the context ends with it
drop(stack);
let closed = close_context(context)?.and(stack_closed);
if closed.is_err() {
shared.shutdown.store(true, Ordering::Release);
}
if let Some(reply) = unload_reply {
let _ = reply.send(closed);
}
answer_unload(shared, close_context(context)?.and(stack_closed), unload_reply);
Ok(())
}
@@ -3,6 +3,7 @@
#[allow(unused_imports)]
use super::*;
mod commit_parts;
mod verify_parts;
use verify_parts::{Round, window_inputs};
mod impl_originalspeccontroller_2;
@@ -0,0 +1,113 @@
//! The commit half of `OriginalSpecController::verify_commit`, moved out of it by STRA-338 (size): the round's verified
//! window is accepted, its prefix committed and the receipt handed back, in the order `verify_commit` did it.
#[allow(unused_imports)]
use super::*;
use super::verify_parts::{Round, WindowPlan};
/// The verified window of a round with the plan it ran under and the number of the round it completes.
pub(super) type Judged<'p, V> = (V, &'p WindowPlan, u64);
/// The window's outputs and how many of its rows the outputs confirm.
fn accepted_prefix<E: OriginalSpecEngine>(
engine: &E,
round: &Round<'_, E>,
verified: &E::Verified,
) -> ([i32; MAX_T], usize) {
let mut outputs = [0; MAX_T];
outputs[..round.t].copy_from_slice(engine.outputs(verified));
let mut accepted = 0;
while accepted < round.t - 1 && round.inputs[accepted + 1] == outputs[accepted] {
accepted += 1;
}
(outputs, accepted)
}
fn note_round_profile(t: usize, accepted: usize, lookup: bool) {
super::super::super::original_serve_profile::add("round.T", t as f64);
super::super::super::original_serve_profile::add("round.acc", accepted as f64);
if lookup {
super::super::super::original_serve_profile::add("round.lookupT", t as f64);
}
}
impl OriginalSpecController {
/// The round may start: the controller is healthy, the request unfinished and not owing a draft, nothing cancelled,
/// and the engine stands where the request does. `false`: the request is finished.
pub(super) fn round_ready<E: OriginalSpecEngine>(
&mut self,
engine: &mut E,
cancel: &mut impl FnMut() -> bool,
) -> SpecResult<bool> {
if self.failed {
return Err(Error::IdentityMismatch.into());
}
let r = self.request.as_ref().ok_or(Error::IdentityMismatch)?;
if r.finished {
return Ok(false);
}
if r.pending_draft {
return Err(Error::IdentityMismatch.into());
}
if cancel() {
return Err(SpecError::Cancelled);
}
engine.validate_current(&r.consumed, r.input, r.position)?;
Ok(true)
}
/// Commit the accepted prefix of the verified window and hand back the receipt. Every failure drains the window,
/// joins the adaptation and restores the checkpoints through `abort`.
pub(super) fn commit_round<E: OriginalSpecEngine>(
&mut self,
engine: &mut E,
round: &Round<'_, E>,
judged: Judged<'_, E::Verified>,
cancel: &mut impl FnMut() -> bool,
) -> SpecResult<Option<CommittedRound<E::Verified, E::AdaptiveWork>>> {
let (verified, plan, next_round) = judged;
if cancel() {
let _ = engine.drain_verified(verified);
return Err(self.abort(engine, round.target, round.draft, SpecError::Cancelled));
}
let (outputs, accepted) = accepted_prefix(engine, round, &verified);
let next = round.p.checked_add(accepted as i64 + 1).ok_or(Error::Overflow)?;
note_round_profile(round.t, accepted, plan.lookup);
let (verified, work) = self.commit_prefix(engine, round, verified, (accepted, next_round))?;
// Actual prefix commit is now authoritative. Adaptation remains beside draft via a receipt owned by the engine;
// finish_round joins it. No borrowed stack worker or unobserved async boolean cert is retained.
let books = self.book_round((round.inputs, &outputs), accepted, next, next_round)?;
// Store the actual pending adaptive owner with the committed receipt.
#[rustfmt::skip]
let receipt = CommittedRound { verified, outputs, emitted: books.emitted, t: round.t, accepted, position: round.p,
from_lookup: plan.lookup, matched: plan.matched, timed: books.timed, eos: books.eos, choice: plan.choice,
next_position: next, round_number: next_round, work: Some(work), owner: Rc::clone(&self.owner),
bookkeeping_error: books.bookkeeping_error };
Ok(Some(receipt))
}
/// Start the adaptation beside the commit, then commit the first `accepted + 1` rows.
fn commit_prefix<E: OriginalSpecEngine>(
&mut self,
engine: &mut E,
round: &Round<'_, E>,
verified: E::Verified,
progress: (usize, u64),
) -> SpecResult<(E::Verified, E::AdaptiveWork)> {
let (accepted, next_round) = progress;
let work = match engine.begin_adaptation(next_round) {
Ok(v) => v,
Err(e) => {
let _ = engine.drain_verified(verified);
return Err(self.abort(engine, round.target, round.draft, e.into()));
}
};
let mut verified = verified;
if let Err(e) = engine.commit_verified(&mut verified, accepted + 1) {
let _ = engine.join_adaptation(work);
let _ = engine.drain_verified(verified);
return Err(self.abort(engine, round.target, round.draft, e.into()));
}
Ok((verified, work))
}
}
@@ -9,83 +9,20 @@ impl OriginalSpecController {
engine: &mut E,
mut cancel: impl FnMut() -> bool,
) -> SpecResult<Option<CommittedRound<E::Verified, E::AdaptiveWork>>> {
if self.failed {
return Err(Error::IdentityMismatch.into());
}
let r = self.request.as_ref().ok_or(Error::IdentityMismatch)?;
if r.finished {
if !self.round_ready(engine, &mut cancel)? {
return Ok(None);
}
if r.pending_draft {
return Err(Error::IdentityMismatch.into());
}
if cancel() {
return Err(SpecError::Cancelled);
}
engine.validate_current(&r.consumed, r.input, r.position)?;
let mut plan = self.plan_window()?;
if !self.fit_window(&mut plan)? {
return Ok(None);
}
let r = self.request.as_ref().ok_or(Error::IdentityMismatch)?;
let p = r.position;
let inputs = window_inputs(r, &plan);
let t = plan.t;
let (p, inputs) = (r.position, window_inputs(r, &plan));
let next_round = self.rounds.checked_add(1).ok_or(Error::Overflow)?;
let target = engine.checkpoint_target()?;
let draft = engine.checkpoint_draft()?;
let round = Round::<E> { p, t, inputs: &inputs, target: &target, draft: &draft };
let mut verified = self.verify_window(engine, &round, &mut cancel)?;
if cancel() {
let _ = engine.drain_verified(verified);
return Err(self.abort(engine, &target, &draft, SpecError::Cancelled));
}
let mut outputs = [0; MAX_T];
outputs[..t].copy_from_slice(engine.outputs(&verified));
let mut accepted = 0;
while accepted < t - 1 && inputs[accepted + 1] == outputs[accepted] {
accepted += 1;
}
let next = p.checked_add(accepted as i64 + 1).ok_or(Error::Overflow)?;
super::super::super::original_serve_profile::add("round.T", t as f64);
super::super::super::original_serve_profile::add("round.acc", accepted as f64);
if plan.lookup {
super::super::super::original_serve_profile::add("round.lookupT", t as f64);
}
let work = match engine.begin_adaptation(next_round) {
Ok(v) => v,
Err(e) => {
let _ = engine.drain_verified(verified);
return Err(self.abort(engine, &target, &draft, e.into()));
}
};
if let Err(e) = engine.commit_verified(&mut verified, accepted + 1) {
let _ = engine.join_adaptation(work);
let _ = engine.drain_verified(verified);
return Err(self.abort(engine, &target, &draft, e.into()));
}
// Actual prefix commit is now authoritative. Adaptation remains beside
// draft via a receipt owned by the engine; finish_round joins it. No
// borrowed stack worker or unobserved async boolean cert is retained.
let books = self.book_round((&inputs, &outputs), accepted, next, next_round)?;
// Store the actual pending adaptive owner with the committed receipt.
Ok(Some(CommittedRound {
verified,
outputs,
emitted: books.emitted,
t,
accepted,
position: p,
from_lookup: plan.lookup,
matched: plan.matched,
timed: books.timed,
eos: books.eos,
choice: plan.choice,
next_position: next,
round_number: next_round,
work: Some(work),
owner: Rc::clone(&self.owner),
bookkeeping_error: books.bookkeeping_error,
}))
let round = Round::<E> { p, t: plan.t, inputs: &inputs, target: &target, draft: &draft };
let verified = self.verify_window(engine, &round, &mut cancel)?;
self.commit_round(engine, &round, (verified, &plan, next_round), &mut cancel)
}
}