fix(serve): keep the draft ring's materialized lower bound across prompt chunks

The second batched chunk of every multi-chunk prompt was refused with
IdentityMismatch at publication: OriginalDraftSession::prepare_publication took
the driver's per-chunk bound (CompletedDraftKv::first_materialized = cell0 + r0,
i.e. the chunk's start) as the ring's whole materialized range and checked it
against end - MTP_ORIGINAL_WINDOW, so [0, start) counted as never recorded.
A chunk that records from its own start where the ring already reaches now
continues the earlier range; a chunk that skipped rows still starts a new one.
Found by job 311 (8000-token prompt, LELEKA_BRINGUP_TRACE): chunk 2 at
first=2048 failed in pending.publish(). The refusal now traces its operands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-16 12:06:10 +02:00
co-authored by Claude Opus 5.5
parent 656925ec86
commit 791a4fa8cb
@@ -155,14 +155,20 @@ impl<'ctx> OriginalDraftSession<'ctx>{
let ring=self.ring.as_ref().ok_or(Error::IdentityMismatch)?;
completed.validate_current(ring).map_err(draft_driver_error)?;
let end=usize::try_from(txn.end_position()).map_err(|_|Error::Overflow)?;
let start=usize::try_from(txn.start_position()).map_err(|_|Error::Overflow)?;
let first_materialized=published_first_materialized(self.first_materialized,self.snapshot.next_position(),
start,completed.first_materialized(),end);
if completed.next_position()!=end||end==0
||completed.first_materialized()>end.saturating_sub(leleka_cuda_driver::MTP_ORIGINAL_WINDOW)
||first_materialized>end.saturating_sub(leleka_cuda_driver::MTP_ORIGINAL_WINDOW)
||!completed.owner_id().same_owner(&self.snapshot.owner_id()){
crate::bringup_trace!("DBG draft.prepare_publication refused: start={start} end={end} completed_next={} completed_first={} prior_first={} prior_next={} lower={first_materialized} same_owner={}",
completed.next_position(),completed.first_materialized(),self.first_materialized,self.snapshot.next_position(),
completed.owner_id().same_owner(&self.snapshot.owner_id()));
return Err(Error::IdentityMismatch);
}
let snapshot=completed.snapshot_after(end-1).map_err(draft_driver_error)?;
ring.validate_snapshot(&snapshot).map_err(draft_driver_error)?;
Ok(OriginalDraftPublication{owner:self,snapshot,first_materialized:completed.first_materialized()})
Ok(OriginalDraftPublication{owner:self,snapshot,first_materialized})
}
/// All physical restores are attempted by the coupled target owner. An
/// uncertain ring restore poisons this owner even when target rollback works.
@@ -248,6 +254,52 @@ impl OriginalDraftPublication<'_, '_>{
}
}
impl Drop for OriginalDraftSession<'_>{fn drop(&mut self){let _=self.close_inner();}}
/// The ring's lower materialized bound after a prompt chunk `[start, end)` whose recording materialized
/// `[chunk_first, end)`. The driver reports the chunk's own bound (`cell0 + r0`); when the chunk starts where
/// the ring's materialized range already reaches (`prior_next >= start`) and recorded from its first row, the
/// range simply continues and keeps the earlier bound. A chunk that skipped its leading rows (the Original's
/// `first_needed` cut) starts a new range at its own bound. Taking the chunk's bound alone made every chunk
/// after the first look as if `[0, start)` had never been recorded, so the second chunk of every multi-chunk
/// prompt was refused (`first_materialized > end - MTP_ORIGINAL_WINDOW`).
fn published_first_materialized(prior_first:usize,prior_next:usize,start:usize,chunk_first:usize,end:usize)->usize{
if chunk_first==start&&prior_next>=start&&prior_first<=start&&start<end {prior_first} else {chunk_first}
}
#[cfg(test)]
mod first_materialized_tests{
use super::published_first_materialized as lower;
use leleka_cuda_driver::MTP_ORIGINAL_WINDOW as W;
/// Walk a whole prompt in chunks the way `prefill_actions` does and check every publication's bound.
fn walk(prompt:usize,chunk:usize)->Vec<(usize,usize)>{
let (mut first,mut next)=(0usize,0usize);let mut out=Vec::new();
let mut start=0;
while start<prompt-1 {let end=(start+chunk).min(prompt-1);
// with_pending_ring: first_needed = end - window; the driver records from max(first_needed, start)
let chunk_first=end.saturating_sub(W).max(start);
first=lower(first,next,start,chunk_first,end);next=end;out.push((first,end));start=end;}
out
}
#[test]
fn contiguous_chunks_keep_the_rings_lower_bound(){
// the 8000-token prompt of job 311: four 2048-row chunks
for (first,end) in walk(8000,2048) {assert_eq!(first,0);assert!(first<=end.saturating_sub(W));}
// the 40000-token prompt of job 310: twenty chunks, past the 32768-cell window
for (first,end) in walk(40000,2048) {assert!(first<=end.saturating_sub(W),"{first} {end}");}
assert_eq!(lower(0,2048,2048,2048,4096),0);
}
#[test]
fn a_chunk_that_skipped_rows_or_left_a_gap_starts_a_new_range(){
assert_eq!(lower(0,2048,2048,2100,4096),2100); // leading rows skipped
assert_eq!(lower(0,1024,2048,2048,4096),2048); // the ring did not reach the chunk's start
assert_eq!(lower(0,0,0,0,2048),0); // first chunk
}
#[test]
fn the_old_rule_refused_the_second_chunk(){
// what publication checked before: the chunk's own bound against the window
let (start,end)=(2048usize,4096usize);
assert!(start>end.saturating_sub(W));
assert!(lower(0,start,start,start,end)<=end.saturating_sub(W));
}
}
fn draft_driver_error(error:leleka_cuda_driver::Error)->Error{match error{
leleka_cuda_driver::Error::ContextMismatch=>Error::IdentityMismatch,
leleka_cuda_driver::Error::InvalidQwenShape|leleka_cuda_driver::Error::InvalidSize=>Error::InvalidDimension,