From 791a4fa8cb09adb3a697719541f259bcdc762ac9 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Fri, 16 Oct 2026 12:06:10 +0200 Subject: [PATCH] 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 --- .../qwen_forward/session_original_draft.rs | 56 ++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/crates/leleka-runtime/src/qwen_forward/session_original_draft.rs b/crates/leleka-runtime/src/qwen_forward/session_original_draft.rs index c11c11fb..503e0f3c 100644 --- a/crates/leleka-runtime/src/qwen_forward/session_original_draft.rs +++ b/crates/leleka-runtime/src/qwen_forward/session_original_draft.rs @@ -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&&startVec<(usize,usize)>{ + let (mut first,mut next)=(0usize,0usize);let mut out=Vec::new(); + let mut start=0; + while startend.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,