diff --git a/crates/vetta-infer-gw/src/serve.rs b/crates/vetta-infer-gw/src/serve.rs index e01e57a..9a96d4b 100644 --- a/crates/vetta-infer-gw/src/serve.rs +++ b/crates/vetta-infer-gw/src/serve.rs @@ -273,6 +273,9 @@ pub fn serve_gen( } } } + if std::env::var("VETTA_DBG_PREFIX").is_ok() { + eprintln!("[prefix] prompt_len={n_prompt} restored={start}"); + } // Resident weight cache for this request: weights upload once and stay on // the GPU across prefill + decode (no per-token NVMe expert re-reads). @@ -309,7 +312,9 @@ pub fn serve_gen( } let prompt_ms = t0.elapsed().as_secs_f64() * 1000.0; // GEN 0 pre-warm: on_prefill_complete emits DONE directly (never decodes). - match sess.on_prefill_complete(0, prompt_ms) { + // `start` is the restored prefix length: >0 emits RESUME (wire-compatible + // with strata, so strata-ctl reports the reused/cached prompt tokens). + match sess.on_prefill_complete(start as u32, prompt_ms) { Ok(lines) => emit_all(emit, lines), Err(e) => { emit_all(emit, sess.on_error(&e)); @@ -317,11 +322,20 @@ pub fn serve_gen( return; } } - if g.max_new == 0 { - // Pre-warm folded the full prompt into `states`: checkpoint it for reuse. - if std::env::var("VETTA_NO_PREFIX_CACHE").is_err() && pos > 0 { - let _ = eng.prefix.borrow_mut().save(&history[..pos as usize], &states, &eng.d); + // Checkpoint the PROMPT boundary. This is the prefix that stays token-identical + // across turns: strata-ctl re-renders the same system+history text each turn, + // while the assistant reply round-trips through detokenize/re-encode (special + // tokens stripped) and is NOT token-identical to the generated ids. Saving here + // — not only at the post-decode `pos` — is what lets the next turn restore the + // stable prefix and prefill only the new dialogue suffix. Mirrors strata + // --auto-ckpt's prompt-boundary save (proofs/prefix_cache.lean restore_equiv). + if std::env::var("VETTA_NO_PREFIX_CACHE").is_err() && pos > 0 { + let _ = eng.prefix.borrow_mut().save(&history[..pos as usize], &states, &eng.d); + if std::env::var("VETTA_DBG_PREFIX").is_ok() { + eprintln!("[prefix] prompt-boundary save seq={pos}"); } + } + if g.max_new == 0 { stop.store(false, Ordering::Relaxed); return; } @@ -397,12 +411,16 @@ pub fn serve_gen( pos += 1; } let decode_ms = t1.elapsed().as_secs_f64() * 1000.0; - // Checkpoint the full processed prefix (prompt + generated tokens) so the next - // turn restores it and prefills only its new suffix. `pos` counts the tokens - // actually folded into `states` (history[..pos]); a sampled token that was not - // forwarded (EOS / last length step) is excluded, keeping the checkpoint valid. + // Also checkpoint the full processed sequence (prompt + generated tokens). This + // is a bonus reuse when the next prompt IS token-identical (e.g. a retry); the + // prompt-boundary checkpoint above is the robust one for normal chat. `pos` + // counts tokens actually folded into `states` (history[..pos]); a sampled token + // not forwarded (EOS / last length step) is excluded, keeping the ckpt valid. if std::env::var("VETTA_NO_PREFIX_CACHE").is_err() && pos > 0 { let _ = eng.prefix.borrow_mut().save(&history[..pos as usize], &states, &eng.d); + if std::env::var("VETTA_DBG_PREFIX").is_ok() { + eprintln!("[prefix] full-sequence save seq={pos}"); + } } emit_all(emit, sess.on_finish(finish, decode_ms).unwrap_or_default()); stop.store(false, Ordering::Relaxed);